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();
}
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");
}
}
}
}
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();
}
}
}
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();
}
}
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();
}
}
}
Aggregations