private void setXAProperties(final AtomikosDataSourceBean dataSourceBean, final String dataSourceName, final XADataSource xaDataSource, final YamlDataSourceParameter dataSourceParameter) throws PropertyException { dataSourceBean.setXaDataSourceClassName(xaDataSource.getClass().getName()); dataSourceBean.setUniqueResourceName(dataSourceName); Properties xaProperties = XAPropertiesFactory.createXAProperties(JDBCURLRecognizerEngine.getDatabaseType(dataSourceParameter.getUrl())).build( new DatabaseAccessConfiguration(dataSourceParameter.getUrl(), dataSourceParameter.getUsername(), dataSourceParameter.getPassword())); PropertyUtils.setProperties(xaDataSource, xaProperties); dataSourceBean.setXaProperties(xaProperties); dataSourceBean.setXaDataSource(xaDataSource); } }
@Override public DataSource build(final String dataSourceName, final YamlDataSourceParameter dataSourceParameter) throws Exception { AtomikosDataSourceBean result = new AtomikosDataSourceBean(); setPoolProperties(result, dataSourceParameter); setXAProperties(result, dataSourceName, XADataSourceFactory.build(DatabaseType.MySQL), dataSourceParameter); return result; }
atomikosDataSourceBean.setXaDataSource(dataSource); atomikosDataSourceBean.setXaDataSourceClassName(getXaDataSourceClassName()); atomikosDataSourceBean.setBorrowConnectionTimeout(getBorrowConnectionTimeout()); if (loginTimeout != 0) { try { atomikosDataSourceBean.setLoginTimeout(getLoginTimeout()); } catch (SQLException e) { log.warn(e.getMessage(), e); atomikosDataSourceBean.setMaxIdleTime(getMaxIdleTime()); atomikosDataSourceBean.setMaxPoolSize(getMaxPoolSize()); atomikosDataSourceBean.setMinPoolSize(getMinPoolSize()); atomikosDataSourceBean.setDefaultIsolationLevel(getDefaultIsolationLevel()); atomikosDataSourceBean.setMaintenanceInterval(getMaintenanceInterval()); atomikosDataSourceBean.setReapTimeout(getReapTimeout()); atomikosDataSourceBean.setTestQuery(getTestQuery()); atomikosDataSourceBean.setXaProperties(getXaProperties()); atomikosDataSourceBean.setMaxLifetime(getMaxLifetime());
private void setPoolProperties(final AtomikosDataSourceBean dataSourceBean, final YamlDataSourceParameter parameter) { dataSourceBean.setMaintenanceInterval((int) (parameter.getMaintenanceIntervalMilliseconds() / 1000)); dataSourceBean.setMinPoolSize(parameter.getMinPoolSize() < 0 ? 0 : parameter.getMinPoolSize()); dataSourceBean.setMaxPoolSize(parameter.getMaxPoolSize()); dataSourceBean.setBorrowConnectionTimeout((int) parameter.getConnectionTimeoutMilliseconds() / 1000); dataSourceBean.setReapTimeout((int) parameter.getMaxLifetimeMilliseconds() / 1000); dataSourceBean.setMaxIdleTime((int) parameter.getIdleTimeoutMilliseconds() / 1000); }
@Override public DataSource wrap(String name, XADataSource xaDataSource, PoolOptions poolOptions, String testQuery) { AtomikosDataSourceBean wrapped = new AtomikosDataSourceBean(); wrapped.setXaDataSource(xaDataSource); wrapped.setUniqueResourceName(name); if (poolOptions == null) { poolOptions = new PoolOptions(); } if (poolOptions.getMaxLifeTime() != null && poolOptions.getMaxLifeTime() != 0) { wrapped.setMaxLifetime(poolOptions.getMaxLifeTime()); } else { // test query is only needed when we don't know how long Atomikos can keep connections in the pool wrapped.setTestQuery(testQuery); } if (poolOptions.getMinPoolSize() != null) { wrapped.setMinPoolSize(poolOptions.getMinPoolSize()); } if (poolOptions.getMaxPoolSize() != null) { wrapped.setMaxPoolSize(poolOptions.getMaxPoolSize()); } if (poolOptions.getMaxIdleTime() != null) { wrapped.setMaxIdleTime(poolOptions.getMaxIdleTime()); } if (poolOptions.getReapTimeout() != null) { wrapped.setReapTimeout(poolOptions.getReapTimeout()); } return wrapped; }
/** * 创建支持XA事务的Atomikos数据源1 */ @Bean public DataSource dataSourceOne(DataSource druidDataSourceOne) { AtomikosDataSourceBean sourceBean = new AtomikosDataSourceBean(); sourceBean.setXaDataSource((DruidXADataSource) druidDataSourceOne); // 必须为数据源指定唯一标识 sourceBean.setUniqueResourceName("db1"); return sourceBean; }
AtomikosDataSourceBean bean = new AtomikosDataSourceBean(); bean.setUniqueResourceName(name); Properties properties = configuration.getProperties(); bean.setXaProperties(properties); bean.setXaDataSourceClassName(configuration.getDriverClass()); setBeanProperties(configuration, bean); registerManagement(bean, configuration.getAliases());
public AtomikosDataSourceFactory() { super.setUniqueResourceName("opennms"); super.setXaDataSource(XADataSourceFactory.getInstance()); super.setPoolSize(30); // Automatically rollback the connection on borrow to avoid a problem where // Atomikos will reuse database connections that contain aborted transactions, // mark the connections as "erroneous", and recycle the connections. We want to // avoid database connection recycling to avoid lockups in PostgreSQL that occur // when creating new connections. This occurs on PostgreSQL 8.4 but may be fixed // in later versions. // // These aborted transactions shouldn't happen and are probably caused by errors // in JDBC code. Atomikos may also only exhibit this behavior when running without // a transaction manager (as is the case in the current OpenNMS code with // Hibernate 3.6). // super.setTestQuery("ROLLBACK;SELECT 1;"); /* // Disable pool maintenance (reaping and shrinking) by setting the interval // to the highest value possible. We want the connections to PostgreSQL to // remain open forever without being recycled. super.setMaintenanceInterval(Integer.MAX_VALUE / 1000); */ }
@Override public void setUniqueResourceName(String resourceName) { super.setUniqueResourceName(resourceName+count++); }
@Override public void setIdleTimeout(int idleTimeout) { super.setMaxIdleTime(idleTimeout); }
@Override public void setMinPool(int minPool) { super.setMinPoolSize(minPool); }
@Override public void setMaxPool(int maxPool) { super.setMaxPoolSize(maxPool); }
private void setPoolProperties(final AtomikosDataSourceBean dataSourceBean, final YamlDataSourceParameter parameter) { dataSourceBean.setMaintenanceInterval((int) (parameter.getMaintenanceIntervalMilliseconds() / 1000)); dataSourceBean.setMinPoolSize(parameter.getMinPoolSize() < 0 ? 0 : parameter.getMinPoolSize()); dataSourceBean.setMaxPoolSize(parameter.getMaxPoolSize()); dataSourceBean.setBorrowConnectionTimeout((int) parameter.getConnectionTimeoutMilliseconds() / 1000); dataSourceBean.setReapTimeout((int) parameter.getMaxLifetimeMilliseconds() / 1000); dataSourceBean.setMaxIdleTime((int) parameter.getIdleTimeoutMilliseconds() / 1000); }
/** * 创建支持XA事务的Atomikos数据源2 */ @Bean public DataSource dataSourceTwo(DataSource druidDataSourceTwo) { AtomikosDataSourceBean sourceBean = new AtomikosDataSourceBean(); sourceBean.setXaDataSource((DruidXADataSource) druidDataSourceTwo); sourceBean.setUniqueResourceName("db2"); return sourceBean; }
AtomikosDataSourceBean bean = new AtomikosDataSourceBean(); bean.setUniqueResourceName(name); Properties properties = configuration.getProperties(); bean.setXaProperties(properties); bean.setXaDataSourceClassName(configuration.getDriverClass()); setBeanProperties(configuration, bean); registerManagement(bean, configuration.getAliases());
atomikosDataSourceBean.setXaDataSource(dataSource); atomikosDataSourceBean.setXaDataSourceClassName(getXaDataSourceClassName()); atomikosDataSourceBean.setBorrowConnectionTimeout(getBorrowConnectionTimeout()); if (loginTimeout != 0) { try { atomikosDataSourceBean.setLoginTimeout(getLoginTimeout()); } catch (SQLException e) { log.warn(e.getMessage(), e); atomikosDataSourceBean.setMaxIdleTime(getMaxIdleTime()); atomikosDataSourceBean.setMaxPoolSize(getMaxPoolSize()); atomikosDataSourceBean.setMinPoolSize(getMinPoolSize()); atomikosDataSourceBean.setDefaultIsolationLevel(getDefaultIsolationLevel()); atomikosDataSourceBean.setMaintenanceInterval(getMaintenanceInterval()); atomikosDataSourceBean.setReapTimeout(getReapTimeout()); atomikosDataSourceBean.setTestQuery(getTestQuery()); atomikosDataSourceBean.setXaProperties(getXaProperties()); atomikosDataSourceBean.setMaxLifetime(getMaxLifetime());
private void setPoolProperties(final DataSourceParameter parameter) { delegate.setMaintenanceInterval((int) (parameter.getMaintenanceIntervalMilliseconds() / 1000)); delegate.setMinPoolSize(parameter.getMinPoolSize() < 0 ? 0 : parameter.getMinPoolSize()); delegate.setMaxPoolSize(parameter.getMaxPoolSize()); delegate.setBorrowConnectionTimeout((int) parameter.getConnectionTimeoutMilliseconds() / 1000); delegate.setReapTimeout((int) parameter.getMaxLifetimeMilliseconds() / 1000); delegate.setMaxIdleTime((int) parameter.getIdleTimeoutMilliseconds() / 1000); }
@Bean(name = "customerDataSource", initMethod = "init", destroyMethod = "close") public DataSource customerDataSource() { JdbcDataSource h2XaDataSource = new JdbcDataSource(); h2XaDataSource.setURL(customerDatasourceProperties.getUrl()); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(h2XaDataSource); xaDataSource.setUniqueResourceName("xads1"); return xaDataSource; }
private void setXAProperties(final AtomikosDataSourceBean dataSourceBean, final String dataSourceName, final XADataSource xaDataSource, final YamlDataSourceParameter dataSourceParameter) throws PropertyException { dataSourceBean.setXaDataSourceClassName(xaDataSource.getClass().getName()); dataSourceBean.setUniqueResourceName(dataSourceName); Properties xaProperties = XAPropertiesFactory.createXAProperties(JDBCURLRecognizerEngine.getDatabaseType(dataSourceParameter.getUrl())).build( new DatabaseAccessConfiguration(dataSourceParameter.getUrl(), dataSourceParameter.getUsername(), dataSourceParameter.getPassword())); PropertyUtils.setProperties(xaDataSource, xaProperties); dataSourceBean.setXaProperties(xaProperties); dataSourceBean.setXaDataSource(xaDataSource); } }
@Override public DataSource build(final String dataSourceName, final YamlDataSourceParameter dataSourceParameter) throws Exception { AtomikosDataSourceBean result = new AtomikosDataSourceBean(); setPoolProperties(result, dataSourceParameter); setXAProperties(result, dataSourceName, XADataSourceFactory.build(DatabaseType.PostgreSQL), dataSourceParameter); return result; }