org.apache.tomcat.jdbc.pool.interceptor.ConnectionState Java Examples

The following examples show how to use org.apache.tomcat.jdbc.pool.interceptor.ConnectionState. 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: TestConnectionState.java    From Tomcat8-Source-Read with MIT License 8 votes vote down vote up
@Test
public void testDefaultCatalog() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultCatalog("information_schema");
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.setCatalog("mysql");
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"mysql");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
}
 
Example #2
Source File: TestConnectionState.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testAutoCommitFalse() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setMinIdle(1);
    d1.setMaxIdle(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.FALSE);
    Connection c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.setAutoCommit(true);
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false for a reused connection",c1.getAutoCommit());
    d1.close(true);
    Assert.assertTrue("Connection should be closed",c1.isClosed());
}
 
Example #3
Source File: TestConnectionState.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCommitFalse() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setMinIdle(1);
    d1.setMaxIdle(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.FALSE);
    Connection c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.setAutoCommit(true);
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false for a reused connection",c1.getAutoCommit());
    d1.close(true);
    Assert.assertTrue("Connection should be closed",c1.isClosed());
}
 
Example #4
Source File: TestConnectionState.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultCatalog() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultCatalog("information_schema");
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.setCatalog("mysql");
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"mysql");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
}
 
Example #5
Source File: TestConnectionState.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCommitFalse() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setMinIdle(1);
    d1.setMaxIdle(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.FALSE);
    Connection c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.setAutoCommit(true);
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertFalse("Auto commit should be false for a reused connection",c1.getAutoCommit());
    d1.close(true);
    Assert.assertTrue("Connection should be closed",c1.isClosed());
}
 
Example #6
Source File: TestConnectionState.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultCatalog() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultCatalog("information_schema");
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.setCatalog("mysql");
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"mysql");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
}
 
Example #7
Source File: PooledConnection.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Connects the underlying connection to the database.
 * @throws SQLException if the method {@link #release()} has been called.
 * @throws SQLException if driver instantiation fails
 * @throws SQLException if a call to {@link java.sql.Driver#connect(String, java.util.Properties)} fails.
 * @throws SQLException if default properties are configured and a call to
 * {@link java.sql.Connection#setAutoCommit(boolean)}, {@link java.sql.Connection#setCatalog(String)},
 * {@link java.sql.Connection#setTransactionIsolation(int)} or {@link java.sql.Connection#setReadOnly(boolean)} fails.
 */
public void connect() throws SQLException {
    if (released.get()) throw new SQLException("A connection once released, can't be reestablished.");
    if (connection != null) {
        try {
            this.disconnect(false);
        } catch (Exception x) {
            log.debug("Unable to disconnect previous connection.", x);
        } //catch
    } //end if
    if (poolProperties.getDataSource()==null && poolProperties.getDataSourceJNDI()!=null) {
        //TODO lookup JNDI name
    }

    if (poolProperties.getDataSource()!=null) {
        connectUsingDataSource();
    } else {
        connectUsingDriver();
    }

    //set up the default state, unless we expect the interceptor to do it
    if (poolProperties.getJdbcInterceptors()==null || poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getName())<0 ||
            poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getSimpleName())<0) {
        if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) connection.setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
        if (poolProperties.getDefaultReadOnly()!=null) connection.setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
        if (poolProperties.getDefaultAutoCommit()!=null) connection.setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
        if (poolProperties.getDefaultCatalog()!=null) connection.setCatalog(poolProperties.getDefaultCatalog());
    }
    this.discarded = false;
    this.lastConnected = System.currentTimeMillis();
}
 
Example #8
Source File: Bug50571.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setUrl("jdbc:h2:~/.h2/test;QUERY_TIMEOUT=0;DB_CLOSE_ON_EXIT=FALSE");
    this.datasource.setJdbcInterceptors(ConnectionState.class.getName());
    this.datasource.setDefaultTransactionIsolation(-55);
    this.datasource.setInitialSize(1);
}
 
Example #9
Source File: TestConnectionState.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testAutoCommitTrue() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.TRUE);
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.setAutoCommit(false);
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true for a reused connection",c1.getAutoCommit());
}
 
Example #10
Source File: PooledConnection.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Connects the underlying connection to the database.
 * @throws SQLException if the method {@link #release()} has been called.
 * @throws SQLException if driver instantiation fails
 * @throws SQLException if a call to {@link java.sql.Driver#connect(String, java.util.Properties)} fails.
 * @throws SQLException if default properties are configured and a call to
 * {@link java.sql.Connection#setAutoCommit(boolean)}, {@link java.sql.Connection#setCatalog(String)},
 * {@link java.sql.Connection#setTransactionIsolation(int)} or {@link java.sql.Connection#setReadOnly(boolean)} fails.
 */
public void connect() throws SQLException {
    if (released.get()) throw new SQLException("A connection once released, can't be reestablished.");
    if (connection != null) {
        try {
            this.disconnect(false);
        } catch (Exception x) {
            log.debug("Unable to disconnect previous connection.", x);
        } //catch
    } //end if
    if (poolProperties.getDataSource()==null && poolProperties.getDataSourceJNDI()!=null) {
        //TODO lookup JNDI name
    }

    if (poolProperties.getDataSource()!=null) {
        connectUsingDataSource();
    } else {
        connectUsingDriver();
    }

    //set up the default state, unless we expect the interceptor to do it
    if (poolProperties.getJdbcInterceptors()==null || poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getName())<0 ||
            poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getSimpleName())<0) {
        if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) connection.setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
        if (poolProperties.getDefaultReadOnly()!=null) connection.setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
        if (poolProperties.getDefaultAutoCommit()!=null) connection.setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
        if (poolProperties.getDefaultCatalog()!=null) connection.setCatalog(poolProperties.getDefaultCatalog());
    }
    this.discarded = false;
    this.lastConnected = System.currentTimeMillis();
}
 
Example #11
Source File: Bug50571.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setUrl("jdbc:h2:~/.h2/test;QUERY_TIMEOUT=0;DB_CLOSE_ON_EXIT=FALSE");
    this.datasource.setJdbcInterceptors(ConnectionState.class.getName());
    this.datasource.setDefaultTransactionIsolation(-55);
    this.datasource.setInitialSize(1);
}
 
Example #12
Source File: TestConnectionState.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCommitTrue() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.TRUE);
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.setAutoCommit(false);
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true for a reused connection",c1.getAutoCommit());
}
 
Example #13
Source File: PooledConnection.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Connects the underlying connection to the database.
 * @throws SQLException if the method {@link #release()} has been called.
 * @throws SQLException if driver instantiation fails
 * @throws SQLException if a call to {@link java.sql.Driver#connect(String, java.util.Properties)} fails.
 * @throws SQLException if default properties are configured and a call to
 * {@link java.sql.Connection#setAutoCommit(boolean)}, {@link java.sql.Connection#setCatalog(String)},
 * {@link java.sql.Connection#setTransactionIsolation(int)} or {@link java.sql.Connection#setReadOnly(boolean)} fails.
 */
public void connect() throws SQLException {
    if (released.get()) throw new SQLException("A connection once released, can't be reestablished.");
    if (connection != null) {
        try {
            this.disconnect(false);
        } catch (Exception x) {
            log.debug("Unable to disconnect previous connection.", x);
        } //catch
    } //end if
    if (poolProperties.getDataSource()==null && poolProperties.getDataSourceJNDI()!=null) {
        //TODO lookup JNDI name
    }

    if (poolProperties.getDataSource()!=null) {
        connectUsingDataSource();
    } else {
        connectUsingDriver();
    }

    //set up the default state, unless we expect the interceptor to do it
    if (poolProperties.getJdbcInterceptors()==null || poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getName())<0 ||
            poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getSimpleName())<0) {
        if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) connection.setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
        if (poolProperties.getDefaultReadOnly()!=null) connection.setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
        if (poolProperties.getDefaultAutoCommit()!=null) connection.setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
        if (poolProperties.getDefaultCatalog()!=null) connection.setCatalog(poolProperties.getDefaultCatalog());
    }
    this.discarded = false;
    this.lastConnected = System.currentTimeMillis();
}
 
Example #14
Source File: Bug50571.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setUrl("jdbc:h2:~/.h2/test;QUERY_TIMEOUT=0;DB_CLOSE_ON_EXIT=FALSE");
    this.datasource.setJdbcInterceptors(ConnectionState.class.getName());
    this.datasource.setDefaultTransactionIsolation(-55);
    this.datasource.setInitialSize(1);
}
 
Example #15
Source File: TestConnectionState.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCommitTrue() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultAutoCommit(Boolean.TRUE);
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true",c1.getAutoCommit());
    c1.setAutoCommit(false);
    Assert.assertFalse("Auto commit should be false",c1.getAutoCommit());
    c1.close();
    c1 = d1.getConnection();
    Assert.assertTrue("Auto commit should be true for a reused connection",c1.getAutoCommit());
}