Java Code Examples for org.apache.commons.dbcp2.PoolableConnectionFactory#setDefaultReadOnly()

The following examples show how to use org.apache.commons.dbcp2.PoolableConnectionFactory#setDefaultReadOnly() . 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: ScipioErrorDbConnectionFactory.java    From scipio-erp with Apache License 2.0 7 votes vote down vote up
private ScipioErrorDbConnectionFactory() {
    Properties properties = new Properties();
    properties.setProperty("user", DB_USERNAME);
    properties.setProperty("password", DB_PASSWORD);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DB_URL, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool); // TODO: REVIEW: circular?
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    poolableConnectionFactory.setValidationQueryTimeout(3);
    poolableConnectionFactory.setDefaultReadOnly(false);
    poolableConnectionFactory.setDefaultAutoCommit(false);
    poolableConnectionFactory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    // Original (log4j manual, dbcp lib):
    //PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
    //        connectionFactory, connectionPool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
    //);
    this.dataSource = new PoolingDataSource<>(connectionPool);
}
 
Example 2
Source File: PoolableConnectionFactoryConfig.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public void init(final PoolableConnectionFactory poolableConnectionFactory) {
    if (poolableConnectionFactory != null) {
        StatusLogger.getLogger().debug("Initializing PoolableConnectionFactory {} with {}",
                poolableConnectionFactory, this);
        poolableConnectionFactory.setCacheState(cacheState);
        poolableConnectionFactory.setConnectionInitSql(connectionInitSqls);
        poolableConnectionFactory.setDefaultAutoCommit(defaultAutoCommit);
        poolableConnectionFactory.setDefaultCatalog(defaultCatalog);
        poolableConnectionFactory.setDefaultQueryTimeout(defaultQueryTimeoutSeconds);
        poolableConnectionFactory.setDefaultReadOnly(defaultReadOnly);
        poolableConnectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
        poolableConnectionFactory.setDisconnectionSqlCodes(disconnectionSqlCodes);
        poolableConnectionFactory.setEnableAutoCommitOnReturn(autoCommitOnReturn);
        poolableConnectionFactory.setFastFailValidation(fastFailValidation);
        poolableConnectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
        poolableConnectionFactory.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
        poolableConnectionFactory.setPoolStatements(poolStatements);
        poolableConnectionFactory.setRollbackOnReturn(rollbackOnReturn);
        poolableConnectionFactory.setValidationQuery(validationQuery);
        poolableConnectionFactory.setValidationQueryTimeout(validationQueryTimeoutSeconds);
    }

}
 
Example 3
Source File: TestManagedDataSource.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    // create a GeronimoTransactionManager for testing
    transactionManager = new TransactionManagerImpl();

    // create a driver connection factory
    final Properties properties = new Properties();
    properties.setProperty("user", "userName");
    properties.setProperty("password", "password");
    final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);

    // wrap it with a LocalXAConnectionFactory
    final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);

    // create the pool object factory
    final PoolableConnectionFactory factory =
        new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);

    // create the pool
    pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setMaxTotal(getMaxTotal());
    pool.setMaxWaitMillis(getMaxWaitMillis());

    // finally create the datasource
    ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry());
    ds.setAccessToUnderlyingConnectionAllowed(true);
}
 
Example 4
Source File: TestManagedConnection.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp()
    throws Exception {
    // create a GeronimoTransactionManager for testing
    transactionManager = new TransactionManagerImpl();

    // create a driver connection factory
    final Properties properties = new Properties();
    properties.setProperty("user", "userName");
    properties.setProperty("password", "password");
    final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);

    // wrap it with a LocalXAConnectionFactory
    final XAConnectionFactory xaConnectionFactory = new UncooperativeLocalXAConnectionFactory(transactionManager, connectionFactory);

    // create the pool object factory
    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);

    // create the pool
    pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setMaxTotal(10);
    pool.setMaxWaitMillis(100);

    // finally create the datasource
    ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry());
    ds.setAccessToUnderlyingConnectionAllowed(true);
}
 
Example 5
Source File: TestPoolableManagedConnection.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    // create a GeronimoTransactionManager for testing
    transactionManager = new TransactionManagerImpl();

    // create a driver connection factory
    final Properties properties = new Properties();
    properties.setProperty("user", "userName");
    properties.setProperty("password", "password");
    final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);

    // wrap it with a LocalXAConnectionFactory
    final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);

    // create transaction registry
    transactionRegistry = xaConnectionFactory.getTransactionRegistry();

    // create the pool object factory
    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);

    // create the pool
    pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setMaxTotal(10);
    pool.setMaxWaitMillis(100);
}
 
Example 6
Source File: TestSynchronizationOrder.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionSynchronization() throws Exception {
    final DataSourceXAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(transactionManager,
            xads);

    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);

    // create the pool
    try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        pool.setMaxTotal(10);
        pool.setMaxWaitMillis(1000);

        // finally create the datasource
        try (final ManagedDataSource ds = new ManagedDataSource<>(pool,
                xaConnectionFactory.getTransactionRegistry())) {
            ds.setAccessToUnderlyingConnectionAllowed(true);

            transactionManager.begin();
            try (final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) ds.getConnection()) {
                // close right away.
            }
            transactionManager.commit();
            assertTrue(transactionManagerRegistered);
            assertFalse(transactionSynchronizationRegistryRegistered);
        }
    }
}
 
Example 7
Source File: TestSynchronizationOrder.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterposedSynchronization() throws Exception {
    final DataSourceXAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(transactionManager,
            xads, transactionSynchronizationRegistry);

    final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);

    // create the pool
    try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) {
        factory.setPool(pool);
        pool.setMaxTotal(10);
        pool.setMaxWaitMillis(1000);

        // finally create the datasource
        try (final ManagedDataSource ds = new ManagedDataSource<>(pool,
                xaConnectionFactory.getTransactionRegistry())) {
            ds.setAccessToUnderlyingConnectionAllowed(true);

            transactionManager.begin();
            try (final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) ds.getConnection()) {
                // Close right away.
            }
            transactionManager.commit();
            assertFalse(transactionManagerRegistered);
            assertTrue(transactionSynchronizationRegistryRegistered);
        }
    }
}
 
Example 8
Source File: DynamicJdbcIO.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
DataSource buildDatasource() {
  BasicDataSource basicDataSource = new BasicDataSource();
  if (getDriverClassName() != null) {
    if (getDriverClassName().get() == null) {
      throw new RuntimeException("Driver class name is required.");
    }
    basicDataSource.setDriverClassName(getDriverClassName().get());
  }
  if (getUrl() != null) {
    if (getUrl().get() == null) {
      throw new RuntimeException("Connection url is required.");
    }
    basicDataSource.setUrl(getUrl().get());
  }
  if (getUsername() != null) {
    basicDataSource.setUsername(getUsername().get());
  }
  if (getPassword() != null) {
    basicDataSource.setPassword(getPassword().get());
  }
  if (getConnectionProperties() != null && getConnectionProperties().get() != null) {
    basicDataSource.setConnectionProperties(getConnectionProperties().get());
  }

  /**
   * Since the jdbc connection connection class might have dependencies that are not available
   * to the template, we will localize the user provided dependencies from GCS and create a new
   * {@link URLClassLoader} that is added to the {@link
   * BasicDataSource#setDriverClassLoader(ClassLoader)}
   */
  if (getDriverJars() != null) {
    ClassLoader urlClassLoader = getNewClassLoader(getDriverJars());
    basicDataSource.setDriverClassLoader(urlClassLoader);
  }

  // wrapping the datasource as a pooling datasource
  DataSourceConnectionFactory connectionFactory =
      new DataSourceConnectionFactory(basicDataSource);
  PoolableConnectionFactory poolableConnectionFactory =
      new PoolableConnectionFactory(connectionFactory, null);
  GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  poolConfig.setMaxTotal(1);
  poolConfig.setMinIdle(0);
  poolConfig.setMinEvictableIdleTimeMillis(10000);
  poolConfig.setSoftMinEvictableIdleTimeMillis(30000);
  GenericObjectPool connectionPool =
      new GenericObjectPool(poolableConnectionFactory, poolConfig);
  poolableConnectionFactory.setPool(connectionPool);
  poolableConnectionFactory.setDefaultAutoCommit(false);
  poolableConnectionFactory.setDefaultReadOnly(false);
  PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);
  return poolingDataSource;
}