Search in sources :

Example 11 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project java-spanner-jdbc by googleapis.

the class ITJdbcReadWriteAutocommitTest method test02_WriteMutation.

@Test
public void test02_WriteMutation() throws Exception {
    try (CloudSpannerJdbcConnection connection = createConnection(env, database)) {
        connection.write(Mutation.newInsertBuilder("TEST").set("ID").to(9999L).set("NAME").to("FOO").build());
        java.sql.Statement statement = connection.createStatement();
        statement.execute(String.format("SHOW VARIABLE %sCOMMIT_TIMESTAMP", getDialect() == Dialect.POSTGRESQL ? "SPANNER." : ""));
        try (java.sql.ResultSet rs = statement.getResultSet()) {
            assertThat(rs.next(), is(true));
            assertThat(rs.getTimestamp(1), is(notNullValue()));
        }
    }
}
Also used : CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 12 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project java-spanner-jdbc by googleapis.

the class ITJdbcConnectTest method testNonDefaultConnection.

private void testNonDefaultConnection(Connection connection) throws SQLException {
    assertThat(connection.isWrapperFor(CloudSpannerJdbcConnection.class)).isTrue();
    CloudSpannerJdbcConnection cs = connection.unwrap(CloudSpannerJdbcConnection.class);
    assertThat(cs.getAutoCommit()).isFalse();
    assertThat(cs.isReadOnly()).isTrue();
    try (ResultSet rs = connection.createStatement().executeQuery("SELECT 1")) {
        assertThat(rs.next()).isTrue();
        assertThat(rs.getInt(1)).isEqualTo(1);
    }
    connection.commit();
    cs.setReadOnly(false);
    assertThat(cs.isRetryAbortsInternally()).isFalse();
}
Also used : ResultSet(java.sql.ResultSet) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection)

Example 13 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project liquibase-spanner by cloudspannerecosystem.

the class CloudSpanner method setConnection.

@Override
public void setConnection(final DatabaseConnection conn) {
    DatabaseConnection connectionToUse = conn;
    // moment.
    if (!(conn instanceof CloudSpannerConnection) && conn instanceof JdbcConnection && ((JdbcConnection) conn).getUnderlyingConnection() instanceof CloudSpannerJdbcConnection) {
        // user-agent string.
        if (!conn.getURL().contains("userAgent=")) {
            // connection that will be used by Liquibase with the correct user-agent.
            try {
                connectionToUse = new CloudSpannerConnection(DriverManager.getConnection(conn.getURL() + ";userAgent=sp-liq"), conn);
            } catch (SQLException e) {
            // Ignore and use the original connection. This could for example happen if the user is
            // using an older version of the Spanner JDBC driver that does not support this user-agent
            // string.
            }
        }
    }
    super.setConnection(connectionToUse);
}
Also used : SQLException(java.sql.SQLException) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection)

Aggregations

CloudSpannerJdbcConnection (com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection)13 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)5 Mutation (com.google.cloud.spanner.Mutation)3 BigDecimal (java.math.BigDecimal)3 Before (org.junit.Before)3 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 JdbcConnection (liquibase.database.jvm.JdbcConnection)2 Test (org.junit.Test)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 Value (com.google.cloud.spanner.Value)1 JdbcAbortedException (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException)1 JdbcSqlScriptVerifier (com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier)1 JdbcGenericConnection (com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier.JdbcGenericConnection)1 TypeCode (com.google.spanner.v1.TypeCode)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1