Java Code Examples for org.apache.tomcat.jdbc.pool.PoolProperties

The following examples show how to use org.apache.tomcat.jdbc.pool.PoolProperties. These examples are extracted from open source projects. 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 Project: Tomcat8-Source-Read   Source File: Bug54978.java    License: MIT License 6 votes vote down vote up
@Test
public void testIllegalValidationQuery() {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(1);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    poolProperties.setTestOnBorrow(true);
    poolProperties.setTestOnConnect(false);
    poolProperties.setValidationQuery("sdadsada");
    final DataSource ds = new DataSource(poolProperties);
    try {
        ds.getConnection().close();
        Assert.fail("Validation should have failed.");
    }catch (SQLException x) {
    }
}
 
Example 2
Source Project: Tomcat8-Source-Read   Source File: Bug54978.java    License: MIT License 6 votes vote down vote up
@Test
public void testIllegalValidationQueryWithLegalInit() throws SQLException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(1);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    poolProperties.setTestOnBorrow(true);
    poolProperties.setTestOnConnect(false);
    poolProperties.setValidationQuery("sdadsada");
    poolProperties.setInitSQL("SELECT 1");
    final DataSource ds = new DataSource(poolProperties);
    ds.getConnection().close();
}
 
Example 3
@Test
public void testBasic() throws Exception {
    String interceptorConfig = "FirstInterceptor;SecondInterceptor(parm1=value1,parm2=value2)";
    PoolProperties props = new PoolProperties();
    props.setJdbcInterceptors(interceptorConfig);
    InterceptorDefinition[] interceptorDefs = props.getJdbcInterceptorsAsArray();
    Assert.assertNotNull(interceptorDefs);

    // 3 items because parser automatically inserts TrapException interceptor to front of list
    Assert.assertEquals(interceptorDefs.length, 3);
    Assert.assertEquals(interceptorDefs[0].getClassName(), TrapException.class.getName());

    Assert.assertNotNull(interceptorDefs[1]);
    Assert.assertEquals(interceptorDefs[1].getClassName(), "FirstInterceptor");
    Assert.assertNotNull(interceptorDefs[2]);
    Assert.assertEquals(interceptorDefs[2].getClassName(), "SecondInterceptor");

    Map<String, InterceptorProperty> secondProps = interceptorDefs[2].getProperties();
    Assert.assertNotNull(secondProps);
    Assert.assertEquals(secondProps.size(), 2);
    Assert.assertNotNull(secondProps.get("parm1"));
    Assert.assertEquals(secondProps.get("parm1").getValue(), "value1");
    Assert.assertNotNull(secondProps.get("parm2"));
    Assert.assertEquals(secondProps.get("parm2").getValue(), "value2");
}
 
Example 4
Source Project: micro-integrator   Source File: DatabaseUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static void setIsolationLevel(PoolProperties poolProperties, String isolationLevelString) {

        if (StringUtils.isNotEmpty(isolationLevelString)) {
            if (JDBCRealmConstants.TX_ISOLATION_LEVELS.NONE.equals(isolationLevelString)) {
                poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
            } else if (JDBCRealmConstants.TX_ISOLATION_LEVELS.READ_UNCOMMITTED.equals(isolationLevelString)) {
                poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            } else if (JDBCRealmConstants.TX_ISOLATION_LEVELS.READ_COMMITTED.equals(isolationLevelString)) {
                poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            } else if (JDBCRealmConstants.TX_ISOLATION_LEVELS.REPEATABLE_READ.equals(isolationLevelString)) {
                poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            } else if (JDBCRealmConstants.TX_ISOLATION_LEVELS.SERIALIZABLE.equals(isolationLevelString)) {
                poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            }
        }
    }
 
Example 5
Source Project: das   Source File: PoolPropertiesHolder.java    License: Apache License 2.0 6 votes vote down vote up
public PoolProperties getPoolProperties(String url, String userName) {
    if (url == null || url.length() == 0) {
        return null;
    }
    if (userName == null || userName.length() == 0) {
        return null;
    }

    url = getShortString(url, SEMICOLON);
    userName = getShortString(userName, AT);
    ConcurrentHashMap<String, PoolProperties> map = poolPropertiesMap.get(url);
    if (map == null) {
        return null;
    }
    return map.get(userName);
}
 
Example 6
Source Project: qmq   Source File: TomcatDataSourceService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public DataSource makeDataSource(String url, String driverClassName, String username, String pwd) {
    PoolConfiguration p = new PoolProperties();
    p.setMinIdle(0);
    p.setMaxActive(2);
    p.setMaxIdle(0);
    p.setInitialSize(0);
    p.setMaxWait(10000);
    p.setDriverClassName(driverClassName);
    p.setUrl(url);
    p.setUsername(username);
    p.setPassword(pwd);
    p.setValidationQuery("select 1");
    p.setTestOnBorrow(true);

    org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    return datasource;
}
 
Example 7
Source Project: dal   Source File: DataSourceValidator.java    License: Apache License 2.0 6 votes vote down vote up
private QueryParameter getQueryParameter(int validateAction) {
    QueryParameter parameter = null;
    if (validateAction != PooledConnection.VALIDATE_INIT)
        return parameter;

    PoolProperties poolProperties = getPoolProperties();
    if (poolProperties == null)
        return parameter;

    String query = poolProperties.getInitSQL();
    int validationQueryTimeout = poolProperties.getValidationQueryTimeout();
    if (validationQueryTimeout <= 0) {
        validationQueryTimeout = DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS;
    }

    parameter = new QueryParameter();
    parameter.setQuery(query);
    parameter.setValidationQueryTimeout(validationQueryTimeout);
    return parameter;
}
 
Example 8
@Override
public org.apache.tomcat.jdbc.pool.DataSource createDataSource(
		DataSourceInformation dataSourceInformation) {
	// create a method scoped instance
	PoolConfiguration configurationToUse = new PoolProperties();

	// copy all general properties
	BeanUtils.copyProperties(this, configurationToUse);

	configurationToUse.setDriverClassName(this.databasePlatformSupport
			.getDriverClassNameForDatabase(dataSourceInformation.getDatabaseType()));
	configurationToUse.setUrl(this.databasePlatformSupport.getDatabaseUrlForDatabase(
			dataSourceInformation.getDatabaseType(),
			dataSourceInformation.getHostName(), dataSourceInformation.getPort(),
			dataSourceInformation.getDatabaseName()));
	configurationToUse.setUsername(dataSourceInformation.getUserName());
	configurationToUse.setPassword(dataSourceInformation.getPassword());

	return new org.apache.tomcat.jdbc.pool.DataSource(configurationToUse);
}
 
Example 9
protected static javax.sql.DataSource createDatasource(Properties properties) {

    PoolProperties p = new PoolProperties();
    p.setUrl(properties.getProperty("databaseUrl"));
    p.setDriverClassName(properties.getProperty("databaseDriver"));
    p.setUsername(properties.getProperty("databaseUser"));
    p.setPassword(properties.getProperty("databasePassword"));

    p.setJmxEnabled(false);

    p.setMaxActive(100);
    p.setInitialSize(10);

    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    return datasource;
  }
 
Example 10
Source Project: Tomcat8-Source-Read   Source File: StatementFinalizer.java    License: MIT License 5 votes vote down vote up
@Override
public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties) {
    super.setProperties(properties);

    PoolProperties.InterceptorProperty logProperty = properties.get("trace");
    if (null != logProperty) {
        logCreationStack = logProperty.getValueAsBoolean(logCreationStack);
    }
}
 
Example 11
Source Project: Tomcat8-Source-Read   Source File: Bug54225.java    License: MIT License 5 votes vote down vote up
@Test
public void testPool() throws SQLException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(0);
    poolProperties.setMaxWait(5000);
    poolProperties.setRemoveAbandoned(true);
    poolProperties.setRemoveAbandonedTimeout(300);
    poolProperties.setRollbackOnReturn(true);
    poolProperties.setInitSQL(initSQL);
    final DataSource ds = new DataSource(poolProperties);
    ds.getConnection().close();
    Assert.assertNull(poolProperties.getInitSQL());
}
 
Example 12
Source Project: Tomcat8-Source-Read   Source File: Bug54227.java    License: MIT License 5 votes vote down vote up
@Test
public void testPool() throws SQLException, InterruptedException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(0);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);

    final DataSource ds = new DataSource(poolProperties);
    Connection con;
    Connection actual1;
    Connection actual2;

    con = ds.getConnection();
    actual1 = ((PooledConnection)con).getConnection();
    con.close();
    con = ds.getConnection();
    actual2 = ((PooledConnection)con).getConnection();
    Assert.assertSame(actual1, actual2);
    con.close();
    Thread.sleep(150);
    con = ds.getConnection();
    actual2 = ((PooledConnection)con).getConnection();
    Assert.assertNotSame(actual1, actual2);
    con.close();
}
 
Example 13
Source Project: micro-integrator   Source File: RDBMSDataSourceUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void handleExternalDataSource(PoolProperties poolProps, RDBMSConfiguration config)
		throws DataSourceException {
	String dsClassName = config.getDataSourceClassName();
	try {
		Object extDataSource = Class.forName(dsClassName).newInstance();
		RDBMSDataSourceUtils.assignBeanProps(extDataSource,
                                                dataSourcePropsToMap(config.getDataSourceProps()));
		poolProps.setDataSource(extDataSource);
	} catch (Exception e) {
		throw new DataSourceException("Error in creating external data source: " +
	            e.getMessage(), e);
	}
}
 
Example 14
Source Project: das   Source File: DataSourceUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static DataSource createDataSource(String url, String userName, String password, String driverClass) throws Exception {
    PoolProperties p = new PoolProperties();
    //System.out.println(url);
    p.setUrl(url);
    p.setUsername(userName);
    p.setPassword(password);
    p.setDriverClassName(driverClass);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationQueryTimeout(5);
    p.setValidationInterval(30000L);
    p.setTimeBetweenEvictionRunsMillis(300000);
    p.setNumTestsPerEvictionRun(50);
    p.setMaxActive(100);
    p.setMinIdle(0);
    p.setMaxWait(10000);
    p.setMaxAge(0L);
    p.setInitialSize(1);
    p.setRemoveAbandonedTimeout(60);
    p.setRemoveAbandoned(true);
    p.setLogAbandoned(true);
    p.setMinEvictableIdleTimeMillis(3600000);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
            + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

    org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(p);
    ds.createPool();
    return ds;
}
 
Example 15
Source Project: das   Source File: DataSourceUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static DataSource createDataSource(String url, String userName, String password, String driverClass)
        throws Exception {
    PoolProperties p = new PoolProperties();

    p.setUrl(url);
    p.setUsername(userName);
    p.setPassword(password);
    p.setDriverClassName(driverClass);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationQueryTimeout(5);
    p.setValidationInterval(30000L);
    p.setTimeBetweenEvictionRunsMillis(5000);
    p.setMaxActive(100);
    p.setMinIdle(0);
    p.setMaxWait(10000);
    p.setMaxAge(0L);
    p.setInitialSize(1);
    p.setRemoveAbandonedTimeout(60);
    p.setRemoveAbandoned(true);
    p.setLogAbandoned(true);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
            + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

    org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(p);
    ds.createPool();
    return ds;
}
 
Example 16
Source Project: das   Source File: MGRConfigReader.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Independent data sources for MGR heart beat.
 */
private void setUpDS() throws Exception {
    Map<String, DalTomcatDataSource> uniq = Maps.newHashMap();
    final ConnectionLocator locator = dasConfigure.getConnectionLocator();
    for (DatabaseSet dbSet : dasConfigure.getDatabaseSets().values()) {
        if(!isMGRCandidate(dbSet)) {
            continue;
        }
        Map<String, DataBase> dbs = dbSet.getDatabases();
        for (DataBase db : dbs.values()) {
            try (Connection conn = locator.getConnection(db.getConnectionString())){
                final String url = conn.getMetaData().getURL();
                final String host = host(url);
                DalTomcatDataSource ds = uniq.get(host);
                boolean duplicate = true;
                if(ds == null){
                    PoolProperties properties = PoolPropertiesHolder.getInstance().getPoolProperties(url, conn.getMetaData().getUserName());
                    PoolProperties poolProperties = mergePoolProperties(properties);
                    poolProperties.setUrl(toMGRCatalog(url));

                    ds = new DalTomcatDataSource(poolProperties);
                    uniq.put(host, ds);
                    duplicate = false;
                }
                connectionString2DS.put(db.getConnectionString(), new DSEntity(ds, host, duplicate));
            } catch (Exception e) {
                logger.error("Exception occurs when setUpDS");
                throw e;
            }
        }
    }
}
 
Example 17
Source Project: das   Source File: DataSourceValidator.java    License: Apache License 2.0 5 votes vote down vote up
private PoolProperties getPoolProperties(Connection connection) {
    String url = null;
    String userName = null;
    try {
        DatabaseMetaData metaData = connection.getMetaData();
        url = metaData.getURL();
        userName = metaData.getUserName();
    } catch (Throwable e) {
        LOGGER.warn("DataSourceValidator getPoolProperties error", e);
        return null;
    }

    return PoolPropertiesHolder.getInstance().getPoolProperties(url, userName);
}
 
Example 18
Source Project: das   Source File: PoolPropertiesHolder.java    License: Apache License 2.0 5 votes vote down vote up
public void setPoolProperties(PoolProperties poolProperties) {
    if (poolProperties == null) {
        return;
    }

    String url = poolProperties.getUrl();
    if (url == null || url.length() == 0) {
        return;
    }

    String userName = poolProperties.getUsername();
    if (userName == null || userName.length() == 0) {
        return;
    }

    url = getShortString(url, SEMICOLON);
    userName = getShortString(userName, AT);
    ConcurrentHashMap<String, PoolProperties> map = poolPropertiesMap.get(url);

    if (map == null) {
        synchronized (LOCK) {
            map = poolPropertiesMap.get(url);
            if (map == null) {
                map = new ConcurrentHashMap<>();
                poolPropertiesMap.put(url, map);
            }
        }
    }

    /*
     * if (!map.containsKey(userName)) { synchronized (LOCK2) { if (!map.containsKey(userName)) { map.put(userName,
     * poolProperties); } } }
     */

    // avoid caching for InitSQL
    synchronized (LOCK2) {
        map.put(userName, poolProperties);
    }
}
 
Example 19
Source Project: das   Source File: PoolPropertiesHelper.java    License: Apache License 2.0 5 votes vote down vote up
public PoolProperties copy(PoolProperties from) {
    PoolProperties properties = new PoolProperties();
    properties.setUrl(from.getUrl());
    properties.setUsername(from.getUsername());
    properties.setPassword(from.getPassword());
    properties.setDriverClassName(from.getDriverClassName());

    properties.setTestWhileIdle(from.isTestWhileIdle());
    properties.setTestOnBorrow(from.isTestOnBorrow());
    properties.setTestOnReturn(from.isTestOnReturn());

    properties.setValidationQuery(from.getValidationQuery());
    properties.setValidationQueryTimeout(from.getValidationQueryTimeout());
    properties.setValidationInterval(from.getValidationInterval());

    properties.setTimeBetweenEvictionRunsMillis(from.getTimeBetweenEvictionRunsMillis());
    properties.setMinEvictableIdleTimeMillis(from.getMinEvictableIdleTimeMillis());

    properties.setMaxAge(from.getMaxAge());
    properties.setMaxActive(from.getMaxActive());
    properties.setMinIdle(from.getMinIdle());
    properties.setMaxWait(from.getMaxWait());
    properties.setInitialSize(from.getInitialSize());

    properties.setRemoveAbandonedTimeout(from.getRemoveAbandonedTimeout());
    properties.setRemoveAbandoned(from.isRemoveAbandoned());
    properties.setLogAbandoned(from.isLogAbandoned());

    properties.setConnectionProperties(from.getConnectionProperties());
    properties.setValidatorClassName(from.getValidatorClassName());

    properties.setInitSQL(from.getInitSQL());
    properties.setJmxEnabled(from.isJmxEnabled());
    properties.setJdbcInterceptors(from.getJdbcInterceptors());

    return properties;
}
 
Example 20
Source Project: dfactor   Source File: DFDbUtil.java    License: MIT License 5 votes vote down vote up
public static DataSource createMysqlDbSource(String url, String user, String pwd, 
		int initSize, int maxActive, int maxWait, int maxIdle, int minIdle){
	PoolProperties p = new PoolProperties();
	p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUrl(url);
    p.setUsername(user);
    p.setPassword(pwd);
    p.setJmxEnabled(true);
    p.setTestWhileIdle(true);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(maxActive);
    p.setInitialSize(initSize);
    p.setMaxWait(maxWait);   //conn timeout 
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMaxIdle(maxIdle);
    p.setMinIdle(minIdle);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
      "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    final DataSource dbSrc = new DataSource();
    dbSrc.setPoolProperties(p);
    return dbSrc;
}
 
Example 21
public static void createPool() throws Exception {
// Modify this code in order to extract a Connection from your
// connection pooling system:

// Driver parameters to use:
String driverClassName = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/sampledb";
String username = "user1";
String password = "password1";

// Creating the DataSource bean and populating the values:
// Mandatory values to set
PoolProperties p = new PoolProperties();
p.setDriverClassName(driverClassName);
p.setUrl(url);
p.setUsername(username);
p.setPassword(password);

// Other possible values to set
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(2);
p.setInitialSize(2);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(2);
p.setMaxIdle(2);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors("ConnectionState;StatementFinalizer");

dataSource = new DataSource();
dataSource.setPoolProperties(p);

   }
 
Example 22
Source Project: Tomcat7.0.67   Source File: SimplePOJOExample.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      con = datasource.getConnection();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example 23
Source Project: metacat   Source File: PoolStatsInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setProperties(final Map<String, PoolProperties.InterceptorProperty> properties) {
    super.setProperties(properties);
    final PoolProperties.InterceptorProperty nameProperty = properties.get(PROP_METRIC_NAME);
    if (nameProperty != null) {
        setMetricName(nameProperty.getValue());
    }
}
 
Example 24
Source Project: gemfirexd-oss   Source File: PooledConnectionTomcat.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static PoolProperties getPoolProperties() {
  Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
  String hostname = getHostNameFromEndpoint(locatorEndPoint);
  int port = getPortFromEndpoint(locatorEndPoint); 
  
  connProp.putAll(getExtraConnProp());
  
  PoolProperties p = new PoolProperties();
 
  StringBuilder sb = new StringBuilder();
  
  for (Iterator iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
    Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
    
    sb.append(entry.getKey() + "=" + entry.getValue() +";");
  }
  int lastIndex = sb.lastIndexOf(";");
  if (lastIndex != -1) sb.deleteCharAt(lastIndex);
  
  p.setConnectionProperties(sb.toString());
  
  Log.getLogWriter().info("Tomcat data source setting the following connection prop: " + sb.toString());
   
  p.setUrl(protocol + hostname+ ":" + port);
  p.setDriverClassName(driver);
  return p;
}
 
Example 25
Source Project: dal   Source File: DatabasePoolConfigParser.java    License: Apache License 2.0 5 votes vote down vote up
public DatabasePoolConfig getDatabasePoolConfig(String name) {
    DataSourceConfigure configure = DataSourceConfigureLocatorManager.getInstance().getDataSourceConfigure(name);
    PoolProperties poolProperties = new PoolProperties();

    poolProperties.setTestWhileIdle(configure.getBooleanProperty(TESTWHILEIDLE, DEFAULT_TESTWHILEIDLE));
    poolProperties.setTestOnBorrow(configure.getBooleanProperty(TESTONBORROW, DEFAULT_TESTONBORROW));
    poolProperties.setTestOnReturn(configure.getBooleanProperty(TESTONRETURN, DEFAULT_TESTONRETURN));

    poolProperties.setValidationQuery(configure.getProperty(VALIDATIONQUERY, DEFAULT_VALIDATIONQUERY));
    poolProperties.setValidationQueryTimeout(
            configure.getIntProperty(VALIDATIONQUERYTIMEOUT, DEFAULT_VALIDATIONQUERYTIMEOUT));
    poolProperties.setValidationInterval(configure.getLongProperty(VALIDATIONINTERVAL, DEFAULT_VALIDATIONINTERVAL));

    poolProperties.setTimeBetweenEvictionRunsMillis(
            configure.getIntProperty(TIMEBETWEENEVICTIONRUNSMILLIS, DEFAULT_TIMEBETWEENEVICTIONRUNSMILLIS));
    poolProperties.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTABLEIDLETIMEMILLIS);

    poolProperties.setMaxAge(configure.getIntProperty(MAX_AGE, DEFAULT_MAXAGE));
    poolProperties.setMaxActive(configure.getIntProperty(MAXACTIVE, DEFAULT_MAXACTIVE));
    poolProperties.setMinIdle(configure.getIntProperty(MINIDLE, DEFAULT_MINIDLE));
    poolProperties.setMaxWait(configure.getIntProperty(MAXWAIT, DEFAULT_MAXWAIT));
    poolProperties.setInitialSize(configure.getIntProperty(INITIALSIZE, DEFAULT_INITIALSIZE));

    poolProperties.setRemoveAbandonedTimeout(
            configure.getIntProperty(REMOVEABANDONEDTIMEOUT, DEFAULT_REMOVEABANDONEDTIMEOUT));
    poolProperties.setRemoveAbandoned(configure.getBooleanProperty(REMOVEABANDONED, DEFAULT_REMOVEABANDONED));
    poolProperties.setLogAbandoned(configure.getBooleanProperty(LOGABANDONED, DEFAULT_LOGABANDONED));

    poolProperties
            .setConnectionProperties(configure.getProperty(CONNECTIONPROPERTIES, DEFAULT_CONNECTIONPROPERTIES));
    poolProperties.setValidatorClassName(configure.getProperty(VALIDATORCLASSNAME, DEFAULT_VALIDATORCLASSNAME));

    poolProperties.setJmxEnabled(DEFAULT_JMXENABLED);
    poolProperties.setJdbcInterceptors(configure.getProperty(JDBC_INTERCEPTORS, DEFAULT_JDBCINTERCEPTORS));

    return new DatabasePoolConfig(poolProperties);
}
 
Example 26
Source Project: dal   Source File: SingleDataSource.java    License: Apache License 2.0 5 votes vote down vote up
private void createDataSource(String name, DataSourceConfigure dataSourceConfigure) {
    try {
        PoolProperties poolProperties = poolPropertiesHelper.convert(dataSourceConfigure);
        setPoolPropertiesIntoValidator(poolProperties);

        final org.apache.tomcat.jdbc.pool.DataSource dataSource = new DalTomcatDataSource(poolProperties);
        this.dataSource = dataSource;
    } catch (Throwable e) {
        LOGGER.error(String.format("Error creating datasource for %s", name), e);
    }
}
 
Example 27
Source Project: dal   Source File: SingleDataSource.java    License: Apache License 2.0 5 votes vote down vote up
private void setPoolPropertiesIntoValidator(PoolProperties poolProperties) {
    if (poolProperties == null)
        return;

    Validator validator = poolProperties.getValidator();
    if (validator == null)
        return;

    if (!(validator instanceof ValidatorProxy))
        return;

    ValidatorProxy dsValidator = (ValidatorProxy) validator;
    dsValidator.setPoolProperties(poolProperties);
}
 
Example 28
Source Project: dal   Source File: DataSourceUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static DataSource createDataSource(String url, String userName, String password, String driverClass)
        throws Exception {
    PoolProperties p = new PoolProperties();

    p.setUrl(url);
    p.setUsername(userName);
    p.setPassword(password);
    p.setDriverClassName(driverClass);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationQueryTimeout(5);
    p.setValidationInterval(30000L);
    p.setTimeBetweenEvictionRunsMillis(5000);
    p.setMaxActive(100);
    p.setMinIdle(0);
    p.setMaxWait(10000);
    p.setMaxAge(0L);
    p.setInitialSize(1);
    p.setRemoveAbandonedTimeout(60);
    p.setRemoveAbandoned(true);
    p.setLogAbandoned(true);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
            + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

    org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(p);
    ds.createPool();
    return ds;
}
 
Example 29
Source Project: tomcatsrc   Source File: SimplePOJOExample.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      con = datasource.getConnection();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example 30
private DataSource getDataSource(DataSourceConfig config) {
    PoolProperties properties = new PoolProperties();
    properties.setUrl(config.getUrl());
    properties.setDriverClassName(config.getDriverClassName());
    properties.setUsername(config.getUser());
    properties.setPassword(config.getPassword());
    return new org.apache.tomcat.jdbc.pool.DataSource(properties);
}