use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcDriver method connect.
@Override
public Connection connect(String url, Properties info) throws SQLException {
if (url != null && url.startsWith("jdbc:cloudspanner")) {
try {
Matcher matcher = URL_PATTERN.matcher(url);
if (matcher.matches()) {
// strip 'jdbc:' from the URL, add any extra properties and pass on to the generic
// Connection API
String connectionUri = appendPropertiesToUrl(url.substring(5), info);
ConnectionOptions options = ConnectionOptions.newBuilder().setUri(connectionUri).build();
JdbcConnection connection = new JdbcConnection(url, options);
if (options.getWarnings() != null) {
connection.pushWarning(new SQLWarning(options.getWarnings()));
}
return connection;
}
} catch (SpannerException e) {
throw JdbcSqlExceptionFactory.of(e);
} catch (IllegalArgumentException e) {
throw JdbcSqlExceptionFactory.of(e.getMessage(), Code.INVALID_ARGUMENT, e);
} catch (Exception e) {
throw JdbcSqlExceptionFactory.of(e.getMessage(), Code.UNKNOWN, e);
}
throw JdbcSqlExceptionFactory.of("invalid url: " + url, Code.INVALID_ARGUMENT);
}
return null;
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project pgadapter by GoogleCloudPlatform.
the class ConnectionHandler method connectToSpanner.
@InternalApi
public void connectToSpanner(String database) {
OptionsMetadata options = getServer().getOptions();
String uri = options.hasDefaultConnectionUrl() ? options.getDefaultConnectionUrl() : options.buildConnectionURL(database);
if (uri.startsWith("jdbc:")) {
uri = uri.substring("jdbc:".length());
}
uri = appendPropertiesToUrl(uri, getServer().getProperties());
if (System.getProperty(CHANNEL_PROVIDER_PROPERTY) != null) {
uri = uri + ";" + ConnectionOptions.CHANNEL_PROVIDER_PROPERTY_NAME + "=" + System.getProperty(CHANNEL_PROVIDER_PROPERTY);
// This forces the connection to use NoCredentials.
uri = uri + ";usePlainText=true";
try {
Class.forName(System.getProperty(CHANNEL_PROVIDER_PROPERTY));
} catch (ClassNotFoundException e) {
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown or invalid channel provider: " + System.getProperty(CHANNEL_PROVIDER_PROPERTY));
}
}
ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
Connection spannerConnection = connectionOptions.getConnection();
try {
// invalid, for example as a result of the credentials being wrong.
if (spannerConnection.getDialect() != Dialect.POSTGRESQL) {
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, String.format("The database uses dialect %s. Currently PGAdapter only supports connections to PostgreSQL dialect databases. " + "These can be created using https://cloud.google.com/spanner/docs/quickstart-console#postgresql", spannerConnection.getDialect()));
}
} catch (SpannerException e) {
spannerConnection.close();
throw e;
}
this.spannerConnection = spannerConnection;
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testHoldability.
@Test
public void testHoldability() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
assertThat(connection.getHoldability()).isEqualTo(ResultSet.CLOSE_CURSORS_AT_COMMIT);
// assert that setting it to this value is ok.
connection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
// assert that setting it to something else is not ok.
int[] settings = new int[] { ResultSet.HOLD_CURSORS_OVER_COMMIT, -100 };
for (int setting : settings) {
boolean exception = false;
try {
connection.setHoldability(setting);
} catch (SQLException e) {
if (setting == -100) {
exception = (e instanceof JdbcSqlException && ((JdbcSqlException) e).getCode() == Code.INVALID_ARGUMENT);
} else {
exception = (e instanceof JdbcSqlException && ((JdbcSqlException) e).getCode() == Code.UNIMPLEMENTED);
}
}
assertThat(exception).isTrue();
}
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testReadOnly.
@Test
public void testReadOnly() {
ConnectionOptions options = mockOptions();
when(options.isAutocommit()).thenReturn(true);
when(options.isReadOnly()).thenReturn(true);
try (Connection connection = createConnection(options)) {
assertThat(connection.isReadOnly()).isTrue();
connection.setReadOnly(false);
assertThat(connection.isReadOnly()).isFalse();
// start a transaction
connection.createStatement().execute("begin transaction");
// setting readonly should no longer be allowed
connection.setReadOnly(true);
fail("missing expected exception");
} catch (SQLException e) {
assertThat(JdbcExceptionMatcher.matchCode(Code.FAILED_PRECONDITION).matches(e)).isTrue();
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method getDefaultClientInfo.
@Test
public void getDefaultClientInfo() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
Properties defaultProperties = connection.getClientInfo();
assertThat(defaultProperties.stringPropertyNames()).containsExactly("APPLICATIONNAME", "CLIENTHOSTNAME", "CLIENTUSER");
}
}
Aggregations