Java Code Examples for java.sql.Connection.getTransactionIsolation()

The following are Jave code examples for showing how to use getTransactionIsolation() of the java.sql.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: alfresco-repository   File: ControlDAOImpl.java   View Source Code Vote up 6 votes
@Override
public int setTransactionIsolationLevel(int isolationLevel)
{
    Connection connection = template.getConnection();
    if (connection == null)
    {
        throw new NullPointerException("There is no current connection");
    }
    try
    {
        if (!connection.getMetaData().supportsTransactionIsolationLevel(isolationLevel))
        {
            throw new IllegalStateException("Transaction isolation level not supported: " + isolationLevel);
        }
        int isolationLevelWas = connection.getTransactionIsolation();
        connection.setTransactionIsolation(isolationLevel);
        return isolationLevelWas;
    }
    catch (SQLException e)
    {
        throw new IllegalStateException("Failed to set transaction isolation level: " + isolationLevel, e);
    }
}
 
Example 2
Project: the-vigilantes   File: ConnectionTest.java   View Source Code Vote up 6 votes
/**
 * Tests whether or not the configuration 'useLocalSessionState' actually
 * prevents non-needed 'set autocommit=', 'set session transaction isolation
 * ...' and 'show variables like tx_isolation' queries.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testUseLocalSessionState() throws Exception {
    Properties props = new Properties();

    props.setProperty("useLocalSessionState", "true");
    props.setProperty("profileSQL", "true");
    props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");

    Connection conn1 = getConnectionWithProps(props);
    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    StandardLogger.startLoggingToBuffer();

    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    conn1.getTransactionIsolation();

    String logAsString = StandardLogger.getBuffer().toString();
    String txIsolationName = versionMeetsMinimum(4, 0, 3) && !versionMeetsMinimum(8, 0, 3) ? "tx_isolation" : "transaction_isolation";

    assertTrue(logAsString.indexOf("SET SESSION") == -1 && logAsString.indexOf("SHOW VARIABLES LIKE '" + txIsolationName + "'") == -1
            && logAsString.indexOf("SET autocommit=") == -1);
}
 
Example 3
Project: the-vigilantes   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for BUG#27655 - getTransactionIsolation() uses
 * "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+
 * 
 * @throws Exception
 */
public void testBug27655() throws Exception {
    Properties props = new Properties();
    props.setProperty("profileSQL", "true");
    props.setProperty("logger", "StandardLogger");
    StandardLogger.startLoggingToBuffer();

    Connection loggedConn = null;

    try {
        loggedConn = getConnectionWithProps(props);
        loggedConn.getTransactionIsolation();

        if (versionMeetsMinimum(8, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'transaction_isolation'"));
        } else if (versionMeetsMinimum(4, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'"));
        }
    } finally {
        StandardLogger.dropBuffer();
        if (loggedConn != null) {
            loggedConn.close();
        }
    }
}
 
Example 4
Project: OpenVertretung   File: ConnectionTest.java   View Source Code Vote up 6 votes
/**
 * Tests whether or not the configuration 'useLocalSessionState' actually
 * prevents non-needed 'set autocommit=', 'set session transaction isolation
 * ...' and 'show variables like tx_isolation' queries.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testUseLocalSessionState() throws Exception {
    Properties props = new Properties();

    props.setProperty("useLocalSessionState", "true");
    props.setProperty("profileSQL", "true");
    props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");

    Connection conn1 = getConnectionWithProps(props);
    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    StandardLogger.startLoggingToBuffer();

    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    conn1.getTransactionIsolation();

    String logAsString = StandardLogger.getBuffer().toString();
    String txIsolationName = versionMeetsMinimum(4, 0, 3) && !versionMeetsMinimum(8, 0, 3) ? "tx_isolation" : "transaction_isolation";

    assertTrue(logAsString.indexOf("SET SESSION") == -1 && logAsString.indexOf("SHOW VARIABLES LIKE '" + txIsolationName + "'") == -1
            && logAsString.indexOf("SET autocommit=") == -1);
}
 
Example 5
Project: OpenVertretung   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for BUG#27655 - getTransactionIsolation() uses
 * "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+
 * 
 * @throws Exception
 */
public void testBug27655() throws Exception {
    Properties props = new Properties();
    props.setProperty("profileSQL", "true");
    props.setProperty("logger", "StandardLogger");
    StandardLogger.startLoggingToBuffer();

    Connection loggedConn = null;

    try {
        loggedConn = getConnectionWithProps(props);
        loggedConn.getTransactionIsolation();

        if (versionMeetsMinimum(8, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'transaction_isolation'"));
        } else if (versionMeetsMinimum(4, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'"));
        }
    } finally {
        StandardLogger.dropBuffer();
        if (loggedConn != null) {
            loggedConn.close();
        }
    }
}
 
Example 6
Project: ProyectoPacientes   File: ConnectionTest.java   View Source Code Vote up 6 votes
/**
 * Tests whether or not the configuration 'useLocalSessionState' actually
 * prevents non-needed 'set autocommit=', 'set session transaction isolation
 * ...' and 'show variables like tx_isolation' queries.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testUseLocalSessionState() throws Exception {
    Properties props = new Properties();

    props.setProperty("useLocalSessionState", "true");
    props.setProperty("profileSQL", "true");
    props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");

    Connection conn1 = getConnectionWithProps(props);
    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    StandardLogger.startLoggingToBuffer();

    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    conn1.getTransactionIsolation();

    String logAsString = StandardLogger.getBuffer().toString();

    assertTrue(logAsString.indexOf("SET SESSION") == -1 && logAsString.indexOf("SHOW VARIABLES LIKE 'tx_isolation'") == -1
            && logAsString.indexOf("SET autocommit=") == -1);
}
 
Example 7
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for BUG#27655 - getTransactionIsolation() uses
 * "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+
 * 
 * @throws Exception
 */
public void testBug27655() throws Exception {
    Properties props = new Properties();
    props.setProperty("profileSQL", "true");
    props.setProperty("logger", "StandardLogger");
    StandardLogger.startLoggingToBuffer();

    Connection loggedConn = null;

    try {
        loggedConn = getConnectionWithProps(props);
        loggedConn.getTransactionIsolation();

        if (versionMeetsMinimum(4, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'"));
        }
    } finally {
        StandardLogger.dropBuffer();
        if (loggedConn != null) {
            loggedConn.close();
        }
    }
}
 
Example 8
Project: uroborosql   File: DataSourceConnectionSupplierImpl.java   View Source Code Vote up 6 votes
/**
 * {@inheritDoc}
 *
 * @param datasourceName 取得するコネクションのデータソース名
 *
 * @see jp.co.future.uroborosql.connection.ConnectionSupplier#getConnection(java.lang.String)
 */
@Override
public Connection getConnection(final String datasourceName) {
	try {
		DataSource ds = dsMap.computeIfAbsent(datasourceName, DataSourceConnectionSupplierImpl::getNewDataSource);
		final Connection connection;
		synchronized (ds) {
			connection = ds.getConnection();
		}
		boolean autoCommit = getAutoCommit(datasourceName);
		if (connection.getAutoCommit() != autoCommit) {
			connection.setAutoCommit(autoCommit);
		}
		boolean readOnly = getReadOnly(datasourceName);
		if (connection.isReadOnly() != readOnly) {
			connection.setReadOnly(readOnly);
		}
		int transactionIsolation = getTransactionIsolation(datasourceName);
		if (transactionIsolation > 0 && connection.getTransactionIsolation() != transactionIsolation) {
			connection.setTransactionIsolation(transactionIsolation);
		}
		return connection;
	} catch (SQLException ex) {
		throw new UroborosqlRuntimeException("Connection[" + datasourceName + "] can not be acquired.", ex);
	}
}
 
Example 9
Project: BibliotecaPS   File: ConnectionTest.java   View Source Code Vote up 6 votes
/**
 * Tests whether or not the configuration 'useLocalSessionState' actually
 * prevents non-needed 'set autocommit=', 'set session transaction isolation
 * ...' and 'show variables like tx_isolation' queries.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testUseLocalSessionState() throws Exception {
    Properties props = new Properties();

    props.setProperty("useLocalSessionState", "true");
    props.setProperty("profileSQL", "true");
    props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");

    Connection conn1 = getConnectionWithProps(props);
    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    StandardLogger.startLoggingToBuffer();

    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    conn1.getTransactionIsolation();

    String logAsString = StandardLogger.getBuffer().toString();
    String txIsolationName = versionMeetsMinimum(4, 0, 3) && !versionMeetsMinimum(8, 0, 3) ? "tx_isolation" : "transaction_isolation";

    assertTrue(logAsString.indexOf("SET SESSION") == -1 && logAsString.indexOf("SHOW VARIABLES LIKE '" + txIsolationName + "'") == -1
            && logAsString.indexOf("SET autocommit=") == -1);
}
 
Example 10
Project: BibliotecaPS   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for BUG#27655 - getTransactionIsolation() uses
 * "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+
 * 
 * @throws Exception
 */
public void testBug27655() throws Exception {
    Properties props = new Properties();
    props.setProperty("profileSQL", "true");
    props.setProperty("logger", "StandardLogger");
    StandardLogger.startLoggingToBuffer();

    Connection loggedConn = null;

    try {
        loggedConn = getConnectionWithProps(props);
        loggedConn.getTransactionIsolation();

        if (versionMeetsMinimum(8, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'transaction_isolation'"));
        } else if (versionMeetsMinimum(4, 0, 3)) {
            assertEquals(-1, StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'"));
        }
    } finally {
        StandardLogger.dropBuffer();
        if (loggedConn != null) {
            loggedConn.close();
        }
    }
}
 
Example 11
Project: sstore-soft   File: ConnectionDefaults.java   View Source Code Vote up 5 votes
public ConnectionDefaults(Connection connection) throws SQLException {

        this.holdability          = connection.getHoldability();
        this.transactionIsolation = connection.getTransactionIsolation();
        this.isAutoCommit         = connection.getAutoCommit();
        this.isReadOnly           = connection.isReadOnly();
        this.catalog              = connection.getCatalog();
    }
 
Example 12
Project: org.ops4j.pax.transx   File: ConnectionWrapper.java   View Source Code Vote up 5 votes
/**
   * Creates a connection wrapper that adds the ability to cache prepared statements.
   *
   * @param connection
   * @param cacheSize
   */
  public ConnectionWrapper(Connection connection, int cacheSize) {
this.connection = connection;
caching = false;
maxCacheSize = cacheSize <= 0 ? 0 : cacheSize;
if (maxCacheSize > 0) {
	caching = true;
	pStmtCache = new HashMap<>(maxCacheSize * 2);
}
      try {
          isolationLevel = connection.getTransactionIsolation();
          isolationCachingEnabled = true;
      } catch (SQLException e) {
      }
  }
 
Example 13
Project: s-store   File: ConnectionDefaults.java   View Source Code Vote up 5 votes
public ConnectionDefaults(Connection connection) throws SQLException {

        this.holdability          = connection.getHoldability();
        this.transactionIsolation = connection.getTransactionIsolation();
        this.isAutoCommit         = connection.getAutoCommit();
        this.isReadOnly           = connection.isReadOnly();
        this.catalog              = connection.getCatalog();
    }
 
Example 14
Project: calcite-avatica   File: ConnectionPropertiesImpl.java   View Source Code Vote up 4 votes
public ConnectionPropertiesImpl(Connection conn) throws SQLException {
  this(conn.getAutoCommit(), conn.isReadOnly(), conn.getTransactionIsolation(),
      conn.getCatalog(), conn.getSchema());
}
 
Example 15
Project: lams   File: LazyConnectionDataSourceProxy.java   View Source Code Vote up 3 votes
/**
 * Check the default connection properties (auto-commit, transaction isolation),
 * keeping them to be able to expose them correctly without fetching an actual
 * JDBC Connection from the target DataSource.
 * <p>This will be invoked once on startup, but also for each retrieval of a
 * target Connection. If the check failed on startup (because the database was
 * down), we'll lazily retrieve those settings.
 * @param con the Connection to use for checking
 * @throws SQLException if thrown by Connection methods
 */
protected synchronized void checkDefaultConnectionProperties(Connection con) throws SQLException {
	if (this.defaultAutoCommit == null) {
		this.defaultAutoCommit = con.getAutoCommit();
	}
	if (this.defaultTransactionIsolation == null) {
		this.defaultTransactionIsolation = con.getTransactionIsolation();
	}
}