List of usage examples for java.sql Connection isReadOnly
boolean isReadOnly() throws SQLException;
Connection
object is in read-only mode. From source file:org.apache.druid.metadata.SQLMetadataConnector.java
protected final <T> T inReadOnlyTransaction(final TransactionCallback<T> callback) { return getDBI().withHandle(new HandleCallback<T>() { @Override/*ww w.ja va 2 s . c o m*/ public T withHandle(Handle handle) throws Exception { final Connection connection = handle.getConnection(); final boolean readOnly = connection.isReadOnly(); connection.setReadOnly(true); try { return handle.inTransaction(callback); } finally { try { connection.setReadOnly(readOnly); } catch (SQLException e) { // at least try to log it so we don't swallow exceptions log.error(e, "Unable to reset connection read-only state"); } } } }); }
From source file:org.apache.metamodel.jdbc.integrationtests.SQLServerJtdsDriverTest.java
public void testWorkingWithDates() throws Exception { if (!isConfigured()) { return;/*from ww w . j ava 2s . c o m*/ } final Connection connection = getConnection(); assertFalse(connection.isReadOnly()); JdbcDataContext dc = new JdbcDataContext(connection); final Schema schema = dc.getSchemaByName("Person"); JdbcTestTemplates.createInsertAndUpdateDateTypes(dc, schema, "test_table"); }
From source file:org.apache.metamodel.jdbc.integrationtests.SQLServerJtdsDriverTest.java
public void testAutomaticConversionWhenInsertingString() throws Exception { if (!isConfigured()) { return;//from www . j a v a 2s . c o m } final Connection connection = getConnection(); assertNotNull(connection); try { // clean up, if nescesary connection.createStatement().execute("DROP TABLE Person.test_table"); } catch (SQLException e) { // do nothing } assertFalse(connection.isReadOnly()); JdbcDataContext dc = new JdbcDataContext(connection); final Schema schema = dc.getSchemaByName("Person"); assertEquals("Person", schema.getName()); dc.executeUpdate(new UpdateScript() { @Override public void run(UpdateCallback cb) { Table table = cb.createTable(schema, "test_table").withColumn("id").asPrimaryKey() .ofType(ColumnType.INTEGER).withColumn("birthdate").ofType(ColumnType.DATE).execute(); cb.insertInto(table).value("id", "1").execute(); cb.insertInto(table).value("id", 2).value("birthdate", "2011-12-21").execute(); } }); Table table = schema.getTableByName("test_table"); assertTrue(table.getColumnByName("id").isPrimaryKey()); assertFalse(table.getColumnByName("birthdate").isPrimaryKey()); // the jdbc driver represents the date as a VARCHAR assertEquals("[Column[name=id,columnNumber=0,type=INTEGER,nullable=false,nativeType=int,columnSize=10], " + "Column[name=birthdate,columnNumber=1,type=VARCHAR,nullable=true,nativeType=date,columnSize=10]]", Arrays.toString(table.getColumns())); DataSet ds = dc.query().from(table).select("id").and("birthdate").execute(); assertTrue(ds.next()); assertEquals("Row[values=[1, null]]", ds.getRow().toString()); assertEquals("java.lang.Integer", ds.getRow().getValue(0).getClass().getName()); assertTrue(ds.next()); assertEquals("Row[values=[2, 2011-12-21]]", ds.getRow().toString()); assertEquals("java.lang.String", ds.getRow().getValue(1).getClass().getName()); assertFalse(ds.next()); ds.close(); connection.createStatement().execute("DROP TABLE Person.test_table"); }
From source file:org.artifactory.storage.db.spring.ArtifactoryPoolableConnectionFactory.java
@Override public void passivateObject(Object obj) throws Exception { if (obj instanceof Connection) { Connection conn = (Connection) obj; if (!conn.getAutoCommit() && !conn.isReadOnly()) { conn.rollback();/*from w w w . java 2 s . co m*/ } conn.clearWarnings(); //if(!conn.getAutoCommit()) { // conn.setAutoCommit(true); //} } if (obj instanceof DelegatingConnection) { Method passivateMethod = ReflectionUtils.findMethod(DelegatingConnection.class, "passivate"); passivateMethod.setAccessible(true); ReflectionUtils.invokeMethod(passivateMethod, obj); } }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Reset the given Connection after a transaction, * regarding read-only flag and isolation level. * @param con the Connection to reset/* w w w. ja v a2 s .co m*/ * @param previousIsolationLevel the isolation level to restore, if any * @see #prepareConnectionForTransaction */ public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel) { Assert.notNull(con, "No Connection specified"); try { // Reset transaction isolation to previous value, if changed for the transaction. if (previousIsolationLevel != null) { if (logger.isDebugEnabled()) { logger.debug("Resetting isolation level of JDBC Connection [" + con + "] to " + previousIsolationLevel); } con.setTransactionIsolation(previousIsolationLevel); } // Reset read-only flag. if (con.isReadOnly()) { if (logger.isDebugEnabled()) { logger.debug("Resetting read-only flag of JDBC Connection [" + con + "]"); } con.setReadOnly(false); } } catch (Throwable ex) { logger.debug("Could not reset JDBC Connection after transaction", ex); } }