Search in sources :

Example 1 with SessionExpiredException

use of org.neo4j.driver.v1.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltCausalClusteringIT method sessionShouldExpireOnLeaderSwitch.

@Test
public void sessionShouldExpireOnLeaderSwitch() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(0).startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
    try (Session session = driver.session()) {
        session.run("CREATE (n:Person {name: 'Jim'})").consume();
        // when
        switchLeader(leader);
        session.run("CREATE (n:Person {name: 'Mark'})").consume();
        fail("Should have thrown exception");
    } catch (SessionExpiredException sep) {
        // then
        assertEquals(String.format("Server at %s no longer accepts writes", leader.boltAdvertisedAddress()), sep.getMessage());
    } finally {
        driver.close();
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 2 with SessionExpiredException

use of org.neo4j.driver.v1.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldHandleLeaderSwitch.

@Test
public void shouldHandleLeaderSwitch() throws Exception {
    // given
    cluster = clusterRule.startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"))) {
        // when
        try (Session session = driver.session()) {
            try (Transaction tx = session.beginTransaction()) {
                switchLeader(leader);
                tx.run("CREATE (person:Person {name: {name}, title: {title}})", parameters("name", "Webber", "title", "Mr"));
                tx.success();
            } catch (SessionExpiredException ignored) {
            // expected
            }
        }
        String bookmark = inExpirableSession(driver, Driver::session, s -> {
            try (Transaction tx = s.beginTransaction()) {
                tx.run("CREATE (person:Person {name: {name}, title: {title}})", parameters("name", "Webber", "title", "Mr"));
                tx.success();
            } catch (SessionExpiredException ignored) {
            }
            return s.lastBookmark();
        });
        // then
        try (Session session = driver.session(AccessMode.READ)) {
            try (Transaction tx = session.beginTransaction(bookmark)) {
                Record record = tx.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
                tx.success();
                assertEquals(1, record.get("count").asInt());
            }
        }
    }
}
Also used : Transaction(org.neo4j.driver.v1.Transaction) CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Record(org.neo4j.driver.v1.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 3 with SessionExpiredException

use of org.neo4j.driver.v1.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldReadAndWriteToANewSessionCreatedAfterALeaderSwitch.

/*
       Create a session with empty arg list (no AccessMode arg), in a driver that was initialized with a bolt+routing
       URI, and ensure that it
       a) works against the Leader for reads and writes before a leader switch, and
       b) receives a SESSION EXPIRED after a leader switch, and
       c) keeps working if a new session is created after that exception, again with no access mode specified.
     */
@Test
public void shouldReadAndWriteToANewSessionCreatedAfterALeaderSwitch() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"))) {
        inExpirableSession(driver, Driver::session, (session) -> {
            // execute a write/read query
            session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
            Record record = session.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
            assertEquals(1, record.get("count").asInt());
            try {
                switchLeader(leader);
                session.run("CREATE (p:Person {name: {name} })").consume();
                fail("Should have thrown an exception as the leader went away mid session");
            } catch (SessionExpiredException sep) {
                // then
                assertEquals(String.format("Server at %s no longer accepts writes", leader.boltAdvertisedAddress()), sep.getMessage());
            } catch (InterruptedException e) {
            // ignored
            }
            return null;
        });
        inExpirableSession(driver, Driver::session, (session) -> {
            // execute a write/read query
            session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
            Record record = session.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
            assertEquals(2, record.get("count").asInt());
            return null;
        });
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Test(org.junit.Test)

Example 4 with SessionExpiredException

use of org.neo4j.driver.v1.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltCausalClusteringIT method sessionShouldExpireOnFailingReadQuery.

@Test
public void sessionShouldExpireOnFailingReadQuery() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
    CoreClusterMember coreServer = cluster.getCoreMemberById(0);
    Driver driver = GraphDatabase.driver(coreServer.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
    inExpirableSession(driver, Driver::session, (session) -> {
        session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
        return null;
    });
    try (Session readSession = driver.session(AccessMode.READ)) {
        // when
        connectedServer(readSession).shutdown();
        // then
        readSession.run("MATCH (n) RETURN n LIMIT 1").consume();
        fail("Should have thrown an exception as the read replica went away mid query");
    } catch (SessionExpiredException sep) {
        // then
        assertThat(sep.getMessage(), containsString("is no longer available"));
    } finally {
        driver.close();
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)4 Driver (org.neo4j.driver.v1.Driver)4 SessionExpiredException (org.neo4j.driver.v1.exceptions.SessionExpiredException)4 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)3 Session (org.neo4j.driver.v1.Session)3 Record (org.neo4j.driver.v1.Record)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Transaction (org.neo4j.driver.v1.Transaction)1