/** * Create DataSource * * @param connection connection * @return data dource */ @Override public DataSource getDataSource(CConnection connection) { if (m_ds != null) return m_ds; try { System.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog"); // System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "ALL"); ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDataSourceName("AdempiereDS"); cpds.setDriverClass(DRIVER); // loads the jdbc driver cpds.setJdbcUrl(getConnectionURL(connection)); cpds.setUser(connection.getDbUid()); cpds.setPassword(connection.getDbPwd()); cpds.setPreferredTestQuery(DEFAULT_CONN_TEST_SQL); cpds.setIdleConnectionTestPeriod(1200); cpds.setAcquireRetryAttempts(2); // cpds.setTestConnectionOnCheckin(true); // cpds.setTestConnectionOnCheckout(true); // cpds.setCheckoutTimeout(60); if (Ini.isClient()) { cpds.setInitialPoolSize(1); cpds.setMinPoolSize(1); cpds.setMaxPoolSize(15); cpds.setMaxIdleTimeExcessConnections(1200); cpds.setMaxIdleTime(900); m_maxbusyconnections = 10; } else { cpds.setInitialPoolSize(10); cpds.setMinPoolSize(5); cpds.setMaxPoolSize(150); cpds.setMaxIdleTimeExcessConnections(1200); cpds.setMaxIdleTime(1200); m_maxbusyconnections = 120; } // the following sometimes kill active connection! // cpds.setUnreturnedConnectionTimeout(1200); // cpds.setDebugUnreturnedConnectionStackTraces(true); m_ds = cpds; } catch (Exception ex) { m_ds = null; // log might cause infinite loop since it will try to acquire database connection again // log.log(Level.SEVERE, "Could not initialise C3P0 Datasource", ex); System.err.println("Could not initialise C3P0 Datasource: " + ex.getLocalizedMessage()); } return m_ds; } // getDataSource
@Bean(name = "mainDataSource") public DataSource dataSource() { ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass(env.getProperty("jdbc.driverClassName")); } catch (PropertyVetoException e) { // TODO Auto-generated catch block logger.info("PropertyVetoException : {}", e.getMessage()); } dataSource.setJdbcUrl(env.getProperty("jdbc.url")); dataSource.setUser(env.getProperty("jdbc.username")); dataSource.setPassword(env.getProperty("jdbc.password")); dataSource.setAcquireIncrement(20); dataSource.setAcquireRetryAttempts(30); dataSource.setAcquireRetryDelay(1000); dataSource.setAutoCommitOnClose(false); dataSource.setDebugUnreturnedConnectionStackTraces(true); dataSource.setIdleConnectionTestPeriod(100); dataSource.setInitialPoolSize(10); dataSource.setMaxConnectionAge(1000); dataSource.setMaxIdleTime(200); dataSource.setMaxIdleTimeExcessConnections(3600); dataSource.setMaxPoolSize(10); dataSource.setMinPoolSize(2); dataSource.setPreferredTestQuery("select 1"); dataSource.setTestConnectionOnCheckin(false); dataSource.setUnreturnedConnectionTimeout(1000); return dataSource; }
/** * Detects changes and reconfigures this dbConfig. Returns true if the database was configured * (Config info present. An exception is throwns if the config process fails. */ protected boolean configure() { // prefix used before all properties when loafing config. default is 'db' String propsPrefix; if (defaultDbConfigName.equals(dbConfigName)) { propsPrefix = "db"; } else { propsPrefix = "db_" + dbConfigName; } boolean dbConfigured = false; if (changed(propsPrefix)) { try { // We now know that we will either config the db, or fail with exception dbConfigured = true; Properties p = Play.configuration; if (datasource != null) { destroy(); } if (p.getProperty(propsPrefix, "").startsWith("java:")) { Context ctx = new InitialContext(); datasource = (DataSource) ctx.lookup(p.getProperty(propsPrefix)); } else { // Try the driver String driver = p.getProperty(propsPrefix + ".driver"); try { Driver d = (Driver) Class.forName(driver, true, Play.classloader).newInstance(); DriverManager.registerDriver(new ProxyDriver(d)); } catch (Exception e) { throw new Exception("Driver not found (" + driver + ")"); } // Try the connection Connection fake = null; try { if (p.getProperty(propsPrefix + ".user") == null) { fake = DriverManager.getConnection(p.getProperty(propsPrefix + ".url")); } else { fake = DriverManager.getConnection( p.getProperty(propsPrefix + ".url"), p.getProperty(propsPrefix + ".user"), p.getProperty(propsPrefix + ".pass")); } } finally { if (fake != null) { fake.close(); } } ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass(p.getProperty(propsPrefix + ".driver")); ds.setJdbcUrl(p.getProperty(propsPrefix + ".url")); ds.setUser(p.getProperty(propsPrefix + ".user")); ds.setPassword(p.getProperty(propsPrefix + ".pass")); ds.setAcquireRetryAttempts(10); ds.setCheckoutTimeout( Integer.parseInt(p.getProperty(propsPrefix + ".pool.timeout", "5000"))); ds.setBreakAfterAcquireFailure(false); ds.setMaxPoolSize(Integer.parseInt(p.getProperty(propsPrefix + ".pool.maxSize", "30"))); ds.setMinPoolSize(Integer.parseInt(p.getProperty(propsPrefix + ".pool.minSize", "1"))); ds.setMaxIdleTimeExcessConnections( Integer.parseInt( p.getProperty(propsPrefix + ".pool.maxIdleTimeExcessConnections", "0"))); ds.setIdleConnectionTestPeriod(10); ds.setTestConnectionOnCheckin(true); datasource = ds; url = ds.getJdbcUrl(); Connection c = null; try { c = ds.getConnection(); } finally { if (c != null) { c.close(); } } Logger.info("Connected to %s", ds.getJdbcUrl()); } destroyMethod = p.getProperty(propsPrefix + ".destroyMethod", ""); } catch (Exception e) { datasource = null; Logger.error( e, "Cannot connected to the database" + getConfigInfoString() + " : %s", e.getMessage()); if (e.getCause() instanceof InterruptedException) { throw new DatabaseException( "Cannot connected to the database" + getConfigInfoString() + ". Check the configuration.", e); } throw new DatabaseException( "Cannot connected to the database" + getConfigInfoString() + ", " + e.getMessage(), e); } } return dbConfigured; }