Search in sources :

Example 6 with CloudSpannerJdbcConnection

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

the class ITJdbcConnectTest method testDefaultConnection.

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

Example 7 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project java-docs-samples by GoogleCloudPlatform.

the class TransactionWithRetryLoopExample method transactionWithRetryLoop.

static void transactionWithRetryLoop(String projectId, String instanceId, String databaseId) throws SQLException {
    // Create a connection that has automatic retry for aborted transactions disabled.
    String connectionUrl = String.format("jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s" + ";retryAbortsInternally=false", projectId, instanceId, databaseId);
    long singerId = 31;
    long albumId = 11;
    try (Connection connection = DriverManager.getConnection(connectionUrl)) {
        while (true) {
            try {
                CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
                spannerConnection.setAutoCommit(false);
                Mutation mutationSingers = Mutation.newInsertBuilder("Singers").set("SingerId").to(singerId).set("FirstName").to("Breanna").set("LastName").to("Fountain").set("Revenues").to(new BigDecimal("29809.93")).build();
                Mutation mutationAlbums = Mutation.newInsertBuilder("Albums").set("SingerId").to(singerId).set("AlbumId").to(albumId).set("AlbumTitle").to("No discounts").set("MarketingBudget").to(1000).build();
                spannerConnection.bufferedWrite(Arrays.asList(mutationSingers, mutationAlbums));
                spannerConnection.commit();
                System.out.printf("Transaction committed at [%s]%n", spannerConnection.getCommitTimestamp().toString());
                break;
            } catch (JdbcAbortedException e) {
                // Rollback the current transaction to initiate a new transaction on the next statement.
                connection.rollback();
                // Transaction aborted, retry.
                System.out.println("Transaction aborted, starting retry");
            }
        }
    }
}
Also used : Connection(java.sql.Connection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) Mutation(com.google.cloud.spanner.Mutation) JdbcAbortedException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException) BigDecimal(java.math.BigDecimal)

Example 8 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project java-docs-samples by GoogleCloudPlatform.

the class BaseJdbcPgExamplesIT method insertTestData.

@Before
public void insertTestData() throws SQLException {
    if (createTestTable()) {
        String connectionUrl = String.format("jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", ServiceOptions.getDefaultProjectId(), instanceId, databaseId);
        try (Connection connection = DriverManager.getConnection(connectionUrl)) {
            if (!testTableCreated) {
                connection.createStatement().execute("CREATE TABLE Singers (\n" + "  SingerId   BIGINT NOT NULL PRIMARY KEY,\n" + "  FirstName  VARCHAR(1024),\n" + "  LastName   VARCHAR(1024),\n" + "  SingerInfo BYTEA,\n" + "  Revenues   NUMERIC\n" + ")\n");
                testTableCreated = true;
            }
            CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
            spannerConnection.setAutoCommit(false);
            for (Singer singer : TEST_SINGERS) {
                spannerConnection.bufferedWrite(Mutation.newInsertBuilder("Singers").set("SingerId").to(singer.singerId).set("FirstName").to(singer.firstName).set("LastName").to(singer.lastName).set("Revenues").to(singer.revenues).build());
            }
            connection.commit();
        }
    }
}
Also used : Connection(java.sql.Connection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) Before(org.junit.Before)

Example 9 with CloudSpannerJdbcConnection

use of com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection in project java-docs-samples by GoogleCloudPlatform.

the class JdbcExamplesIT method insertTestData.

@Before
public void insertTestData() throws SQLException {
    String connectionUrl = String.format("jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", ServiceOptions.getDefaultProjectId(), instanceId, databaseId);
    try (Connection connection = DriverManager.getConnection(connectionUrl)) {
        CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
        spannerConnection.setAutoCommit(false);
        for (Singer singer : TEST_SINGERS) {
            spannerConnection.bufferedWrite(Mutation.newInsertBuilder("Singers").set("SingerId").to(singer.singerId).set("FirstName").to(singer.firstName).set("LastName").to(singer.lastName).set("Revenues").to(singer.revenues).build());
        }
        connection.commit();
    }
}
Also used : Connection(java.sql.Connection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) Before(org.junit.Before)

Example 10 with CloudSpannerJdbcConnection

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

the class ITJdbcReadOnlyTest method createTestTables.

@Before
public void createTestTables() throws Exception {
    try (CloudSpannerJdbcConnection connection = createConnection(env, database)) {
        if (!(tableExists(connection, "NUMBERS") && tableExists(connection, "PRIME_NUMBERS"))) {
            // create tables
            JdbcSqlScriptVerifier verifier = new JdbcSqlScriptVerifier(new ITJdbcConnectionProvider(env, database));
            verifier.verifyStatementsInFile(dialect.createTableFile, JdbcSqlScriptVerifier.class, false);
            // fill tables with data
            connection.setAutoCommit(false);
            connection.setReadOnly(false);
            for (long number = 1L; number <= TEST_ROWS_COUNT; number++) {
                connection.bufferedWrite(Mutation.newInsertBuilder("NUMBERS").set("number").to(number).set("name").to(Long.toBinaryString(number)).build());
            }
            for (long number = 1L; number <= TEST_ROWS_COUNT; number++) {
                if (BigInteger.valueOf(number).isProbablePrime(Integer.MAX_VALUE)) {
                    connection.bufferedWrite(Mutation.newInsertBuilder("PRIME_NUMBERS").set("prime_number").to(number).set("binary_representation").to(Long.toBinaryString(number)).build());
                }
            }
            connection.commit();
        }
    }
}
Also used : JdbcSqlScriptVerifier(com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) Before(org.junit.Before)

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