Java Code Examples for java.sql.DatabaseMetaData#supportsTransactionIsolationLevel()
The following examples show how to use
java.sql.DatabaseMetaData#supportsTransactionIsolationLevel() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionTest.java From r-course with MIT License | 6 votes |
/** * Tests isolation level functionality * * @throws Exception * if an error occurs */ public void testIsolationLevel() throws Exception { if (versionMeetsMinimum(4, 0)) { String[] isoLevelNames = new String[] { "Connection.TRANSACTION_NONE", "Connection.TRANSACTION_READ_COMMITTED", "Connection.TRANSACTION_READ_UNCOMMITTED", "Connection.TRANSACTION_REPEATABLE_READ", "Connection.TRANSACTION_SERIALIZABLE" }; int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE }; DatabaseMetaData dbmd = this.conn.getMetaData(); for (int i = 0; i < isolationLevels.length; i++) { if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) { this.conn.setTransactionIsolation(isolationLevels[i]); assertTrue( "Transaction isolation level that was set (" + isoLevelNames[i] + ") was not returned, nor was a more restrictive isolation level used by the server", this.conn.getTransactionIsolation() == isolationLevels[i] || this.conn.getTransactionIsolation() > isolationLevels[i]); } } } }
Example 2
Source File: Transaction.java From kamike.divide with GNU Lesser General Public License v3.0 | 6 votes |
protected void setTransactionIsolationLevel(int transactionIsolationLevel) { try { if (con != null) { DatabaseMetaData dbmt = con.getMetaData(); if (dbmt.supportsTransactions()) { if (dbmt.supportsTransactionIsolationLevel(transactionIsolationLevel)) { originalTransactionIsolationLevel = con.getTransactionIsolation(); con.setTransactionIsolation(transactionIsolationLevel); } } } } catch (SQLException ex) { Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex); } }
Example 3
Source File: Transaction.java From kamike.divide with GNU Lesser General Public License v3.0 | 6 votes |
protected void resetTransactionIsolationLevel() { try { if (con != null) { DatabaseMetaData dbmt = con.getMetaData(); if (dbmt.supportsTransactions()) { if (dbmt.supportsTransactionIsolationLevel(originalTransactionIsolationLevel)) { con.setTransactionIsolation(originalTransactionIsolationLevel); } } } } catch (SQLException ex) { Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex); } }
Example 4
Source File: JDBCRepository.java From Carbonado with Apache License 2.0 | 6 votes |
/** * Returns the highest supported level for the given desired level. * * @return null if not supported */ private static IsolationLevel selectIsolationLevel(DatabaseMetaData md, IsolationLevel desiredLevel) throws SQLException, RepositoryException { while (!md.supportsTransactionIsolationLevel(mapIsolationLevelToJdbc(desiredLevel))) { switch (desiredLevel) { case READ_UNCOMMITTED: desiredLevel = IsolationLevel.READ_COMMITTED; break; case READ_COMMITTED: desiredLevel = IsolationLevel.REPEATABLE_READ; break; case REPEATABLE_READ: desiredLevel = IsolationLevel.SERIALIZABLE; break; case SNAPSHOT: desiredLevel = IsolationLevel.SERIALIZABLE; break; case SERIALIZABLE: default: return null; } } return desiredLevel; }
Example 5
Source File: JdbcBasePlugin.java From pxf with Apache License 2.0 | 5 votes |
/** * Prepare JDBC connection by setting session-level variables in external database * * @param connection {@link Connection} to prepare */ private void prepareConnection(Connection connection) throws SQLException { if (connection == null) { throw new IllegalArgumentException("The provided connection is null"); } DatabaseMetaData metadata = connection.getMetaData(); // Handle optional connection transaction isolation level if (transactionIsolation != TransactionIsolation.NOT_PROVIDED) { // user wants to set isolation level explicitly if (metadata.supportsTransactionIsolationLevel(transactionIsolation.getLevel())) { LOG.debug("Setting transaction isolation level to {} on connection {}", transactionIsolation.toString(), connection); connection.setTransactionIsolation(transactionIsolation.getLevel()); } else { throw new RuntimeException( String.format("Transaction isolation level %s is not supported", transactionIsolation.toString()) ); } } // Disable autocommit if (metadata.supportsTransactions()) { LOG.debug("Setting autoCommit to false on connection {}", connection); connection.setAutoCommit(false); } // Prepare session (process sessionConfiguration) if (!sessionConfiguration.isEmpty()) { DbProduct dbProduct = DbProduct.getDbProduct(metadata.getDatabaseProductName()); try (Statement statement = connection.createStatement()) { for (Map.Entry<String, String> e : sessionConfiguration.entrySet()) { String sessionQuery = dbProduct.buildSessionQuery(e.getKey(), e.getValue()); LOG.debug("Executing statement {} on connection {}", sessionQuery, connection); statement.execute(sessionQuery); } } } }
Example 6
Source File: JDBCLinkStore.java From qpid-broker-j with Apache License 2.0 | 5 votes |
private void checkTransactionIsolationLevel() throws SQLException { try (Connection connection = getConnection()) { DatabaseMetaData metaData = connection.getMetaData(); if (!metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)) { throw new StoreException(String.format( "The RDBMS '%s' does not support required transaction isolation level 'serializable'", metaData.getDatabaseProductName())); } } }
Example 7
Source File: ConnectionTest.java From Komondor with GNU General Public License v3.0 | 5 votes |
/** * Tests isolation level functionality * * @throws Exception * if an error occurs */ public void testIsolationLevel() throws Exception { if (versionMeetsMinimum(4, 0)) { // Check initial transaction isolation level ((ConnectionProperties) this.conn).setUseLocalSessionState(true); int initialTransactionIsolation = this.conn.getTransactionIsolation(); ((ConnectionProperties) this.conn).setUseLocalSessionState(false); int actualTransactionIsolation = this.conn.getTransactionIsolation(); assertEquals("Inital transaction isolation level doesn't match the server's", actualTransactionIsolation, initialTransactionIsolation); // Check setting all allowed transaction isolation levels String[] isoLevelNames = new String[] { "Connection.TRANSACTION_NONE", "Connection.TRANSACTION_READ_COMMITTED", "Connection.TRANSACTION_READ_UNCOMMITTED", "Connection.TRANSACTION_REPEATABLE_READ", "Connection.TRANSACTION_SERIALIZABLE" }; int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE }; DatabaseMetaData dbmd = this.conn.getMetaData(); for (int i = 0; i < isolationLevels.length; i++) { if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) { this.conn.setTransactionIsolation(isolationLevels[i]); assertTrue( "Transaction isolation level that was set (" + isoLevelNames[i] + ") was not returned, nor was a more restrictive isolation level used by the server", this.conn.getTransactionIsolation() == isolationLevels[i] || this.conn.getTransactionIsolation() > isolationLevels[i]); } } } }
Example 8
Source File: ConnectionTest.java From FoxTelem with GNU General Public License v3.0 | 5 votes |
/** * Tests isolation level functionality * * @throws Exception * if an error occurs */ public void testIsolationLevel() throws Exception { // Check initial transaction isolation level ((MysqlConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.useLocalSessionState).setValue(true); int initialTransactionIsolation = this.conn.getTransactionIsolation(); ((MysqlConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.useLocalSessionState).setValue(false); int actualTransactionIsolation = this.conn.getTransactionIsolation(); assertEquals("Inital transaction isolation level doesn't match the server's", actualTransactionIsolation, initialTransactionIsolation); // Check setting all allowed transaction isolation levels String[] isoLevelNames = new String[] { "Connection.TRANSACTION_NONE", "Connection.TRANSACTION_READ_COMMITTED", "Connection.TRANSACTION_READ_UNCOMMITTED", "Connection.TRANSACTION_REPEATABLE_READ", "Connection.TRANSACTION_SERIALIZABLE" }; int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE }; DatabaseMetaData dbmd = this.conn.getMetaData(); for (int i = 0; i < isolationLevels.length; i++) { if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) { this.conn.setTransactionIsolation(isolationLevels[i]); assertTrue( "Transaction isolation level that was set (" + isoLevelNames[i] + ") was not returned, nor was a more restrictive isolation level used by the server", this.conn.getTransactionIsolation() == isolationLevels[i] || this.conn.getTransactionIsolation() > isolationLevels[i]); } } }