Search in sources :

Example 1 with CloudSpannerXAConnection

use of nl.topicus.jdbc.xa.CloudSpannerXAConnection in project spanner-jdbc by olavloite.

the class XATester method testXATransaction.

private void testXATransaction(CloudSpannerXAConnection xaConnection, CommitMode mode) throws SQLException, XAException {
    log.info("Starting XA simple transaction test");
    Connection connection = xaConnection.getConnection();
    Xid xid = getRandomXid();
    xaConnection.start(xid, XAResource.TMNOFLAGS);
    String sql = "insert into test (id, uuid, active, amount, description, created_date, last_updated) values (?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement statement = connection.prepareStatement(sql);
    setParameterValues(statement, 1000000);
    statement.executeUpdate();
    xaConnection.end(xid, XAResource.TMSUCCESS);
    xaConnection.prepare(xid);
    if (mode != CommitMode.None) {
        xaConnection.commit(xid, mode == CommitMode.OnePhase);
    }
    if (mode != CommitMode.None) {
        boolean found = false;
        try (ResultSet rs = connection.createStatement().executeQuery("select * from test where id=1000000")) {
            if (rs.next())
                found = true;
        }
        Assert.assertTrue(found);
    }
    log.info("Finished XA simple transaction test");
}
Also used : RecoveredXid(nl.topicus.jdbc.xa.RecoveredXid) Xid(javax.transaction.xa.Xid) Connection(java.sql.Connection) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with CloudSpannerXAConnection

use of nl.topicus.jdbc.xa.CloudSpannerXAConnection in project spanner-jdbc by olavloite.

the class XATester method testXA.

public void testXA(String projectId, String instanceId, String database, String pvtKeyPath) throws SQLException {
    log.info("Starting XA tests");
    int originalLogLevel = CloudSpannerDriver.getLogLevel();
    CloudSpannerDriver.setLogLevel(CloudSpannerDriver.DEBUG);
    CloudSpannerXADataSource ds = new CloudSpannerXADataSource();
    ds.setProjectId(projectId);
    ds.setInstanceId(instanceId);
    ds.setDatabase(database);
    ds.setPvtKeyPath(pvtKeyPath);
    ds.setAllowExtendedMode(true);
    try (CloudSpannerXAConnection xaConnection = ds.getXAConnection()) {
        testXATransaction(xaConnection, CommitMode.TwoPhase);
        testXARollback(xaConnection);
        deleteTestRow(xaConnection);
        testXARecover(xaConnection);
        deleteTestRow(xaConnection);
    } catch (Exception e) {
        throw new CloudSpannerSQLException("Exception occurred during XA tests", Code.INTERNAL, e);
    } finally {
        CloudSpannerDriver.setLogLevel(originalLogLevel);
    }
    log.info("Finished XA tests");
}
Also used : CloudSpannerXADataSource(nl.topicus.jdbc.CloudSpannerXADataSource) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SQLException(java.sql.SQLException) XAException(javax.transaction.xa.XAException)

Example 3 with CloudSpannerXAConnection

use of nl.topicus.jdbc.xa.CloudSpannerXAConnection in project spanner-jdbc by olavloite.

the class XATester method prepareDeleteRow.

private Xid prepareDeleteRow(CloudSpannerXAConnection xaConnection) throws SQLException, XAException {
    Connection connection = xaConnection.getConnection();
    Xid xid = getRandomXid();
    xaConnection.start(xid, XAResource.TMNOFLAGS);
    String sql = "delete from test where id=1000000";
    PreparedStatement statement = connection.prepareStatement(sql);
    statement.executeUpdate();
    xaConnection.end(xid, XAResource.TMSUCCESS);
    xaConnection.prepare(xid);
    return xid;
}
Also used : RecoveredXid(nl.topicus.jdbc.xa.RecoveredXid) Xid(javax.transaction.xa.Xid) Connection(java.sql.Connection) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CloudSpannerXAConnection (nl.topicus.jdbc.xa.CloudSpannerXAConnection)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Xid (javax.transaction.xa.Xid)2 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 XAException (javax.transaction.xa.XAException)1 CloudSpannerXADataSource (nl.topicus.jdbc.CloudSpannerXADataSource)1 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)1