Skip to content

Atomikos TransactionsEssentials Configuration

Vlad Mihalcea edited this page Dec 17, 2019 · 5 revisions

Maven dependency

Traditionally, the flexy-atomikos is the dependency you should use for Atomikos TranscationEssentials.

<dependency>
	<groupId>com.vladmihalcea.flexy-pool</groupId>
	<artifactId>flexy-atomikos</artifactId>
	<version>${flexy-pool.version}</version>
</dependency>

However, since [version 5](https://www.atomikos.com/Blog/ExtremeTransactions5dot0), Atomikos has changed the package names of the AtomikosDataSourceBean, hence FlexyPool 2.2.0 added support for Atomikos 5 while maintaining backward compatibility with Atomikos 4 or previous versions.

Maven dependency for Atomikos 5

If you’re using Atomikos 5, it’s much more efficient to set this dependency instead of the legacy flexy-atomikos:

<dependency>
	<groupId>com.vladmihalcea.flexy-pool</groupId>
	<artifactId>flexy-atomikos5</artifactId>
	<version>${flexy-pool.version}</version>
</dependency>

Maven dependency for Atomikos 4 or older

Also, for Atomikos 4 or older releases, it’s much more efficient to use the flexy-atomikos4 dependency:

<dependency>
	<groupId>com.vladmihalcea.flexy-pool</groupId>
	<artifactId>flexy-atomikos4</artifactId>
	<version>${flexy-pool.version}</version>
</dependency>

The difference between using the flexy-atomikos on one hand or the flexy-atomikos5 or flexy-atomikos4 on the other lies in the PoolAdapter implementation you have to use.

Therefore, you can match the PoolAdapter implementation with the Maven dependency you configured according to the following compatibility matrix:

flexy-atomikos

For the legacy dependency, you can use AtomikosPoolAdapter, which will delegate calls to either Atomikos5PoolAdapter or Atomikos4PoolAdapter based on the type of the provided target DataSource.

flexy-atomikos5

For the Atomikos 5-specific dependency, you can use the Atomikos5PoolAdapter.

flexy-atomikos4

For the Atomikos 4-specific dependency, you can use the Atomikos4PoolAdapter.

The advantage of using either Atomikos5PoolAdapter or Atomikos4PoolAdapter is that FlexyPool does not need to delegate calls from the AtomikosPoolAdapter, therefore increasing performance.

DataSource configuration

Assuming you are using the following AtomikosDataSourceBean:

<bean id="poolingDataSource"
      class="com.atomikos.jdbc.AtomikosDataSourceBean"
      init-method="init"
      destroy-method="close">
    <property name="uniqueResourceName" value="testDataSource"/>
    <property name="xaDataSourceClassName" value="org.hsqldb.jdbc.pool.JDBCXADataSource"/>
    <property name="minPoolSize" value="1"/>
    <property name="maxPoolSize" value="3"/>
    <property name="borrowConnectionTimeout" value="1"/>
    <property name="xaProperties">
        <props>
            <prop key="user">${jdbc.username}</prop>
            <prop key="password">${jdbc.password}</prop>
            <prop key="url">${jdbc.url}</prop>
        </props>
    </property>
</bean>

The FlexyPool Atomikos configuration depends on the FlexyPool Atomikos dependency you have configured in your project.

The flexypool-atomikos configuration

If you’re using the flexypool-atomikos dependency, you can configure FlexyPool using the following Spring Java-Bean Configuration:

@org.springframework.context.annotation.Configuration
public class FlexyPoolConfiguration {

    @Autowired
    private DataSource poolingDataSource;

    @Value("${flexy.pool.uniqueId}")
    private String uniqueId;

    @Bean
    public Configuration<DataSource> configuration() {
        return new Configuration.Builder<DataSource>(
                uniqueId,
                poolingDataSource,
                AtomikosPoolAdapter.FACTORY
        )
        .setJmxEnabled(true)
        .setMetricLogReporterMillis(TimeUnit.SECONDS.toMillis(5))
        .build();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public FlexyPoolDataSource dataSource() {
        Configuration<DataSource> configuration = configuration();
        return new FlexyPoolDataSource<DataSource>(configuration,
                new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
                new RetryConnectionAcquiringStrategy.Factory(2)
        );
    }
}

Or you could use the legacy XML-based Spring configuration:

Spring XML Configuration

<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder">
    <constructor-arg value="uniqueId"/>
    <constructor-arg ref="poolingDataSource"/>
    <constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.AtomikosPoolAdapter).FACTORY }"/>
</bean>

<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/>

<bean id="dataSource" class="com.vladmihalcea.flexypool.FlexyPoolDataSource" init-method="start" destroy-method="stop">
    <constructor-arg ref="configuration"/>
    <constructor-arg>
        <array>
            <bean class="com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory">
                <constructor-arg value="5"/>
            </bean>
            <bean class="com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy.Factory">
                <constructor-arg value="2"/>
            </bean>
        </array>
    </constructor-arg>
</bean>

The flexypool-atomikos5 configuration

For the Atomikos 5 dependency, the Java-based configuration can use the Atomikos 5-specific com.atomikos.jdbc.internal.AbstractDataSourceBean which is extended by either the JTA-specific AtomikosDataSourceBean or the RESOURCE_LOCAL-specific AtomikosNonXADataSourceBean.

Also, since we are using Atomikos 5, we can use the Atomikos5PoolAdapter instead of the legacy AtomikosPoolAdapter:

@org.springframework.context.annotation.Configuration
public class FlexyPoolConfiguration {

    @Autowired
    private AbstractDataSourceBean poolingDataSource;

    @Value("${flexy.pool.uniqueId}")
    private String uniqueId;

    @Bean
    public Configuration<AbstractDataSourceBean> configuration() {
        return new Configuration.Builder<AbstractDataSourceBean>(
                uniqueId,
                poolingDataSource,
                Atomikos5PoolAdapter.FACTORY
        )
        .setJmxEnabled(true)
        .setMetricLogReporterMillis(TimeUnit.SECONDS.toMillis(5))
        .build();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public FlexyPoolDataSource dataSource() {
        Configuration<AbstractDataSourceBean> configuration = configuration();
        return new FlexyPoolDataSource<AbstractDataSourceBean>(configuration,
                new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
                new RetryConnectionAcquiringStrategy.Factory(2)
        );
    }
}

The flexypool-atomikos4 configuration

For the Atomikos 4 dependency, the Java-based configuration can use the Atomikos 4-specific com.atomikos.jdbc.AbstractDataSourceBean which is extended by either the JTA-specific AtomikosDataSourceBean or the RESOURCE_LOCAL-specific AtomikosNonXADataSourceBean.

Also, since we are using Atomikos 4, we can use the Atomikos4PoolAdapter instead of the legacy AtomikosPoolAdapter:

@org.springframework.context.annotation.Configuration
public class FlexyPoolConfiguration {

    @Autowired
    private AbstractDataSourceBean poolingDataSource;

    @Value("${flexy.pool.uniqueId}")
    private String uniqueId;

    @Bean
    public Configuration<AbstractDataSourceBean> configuration() {
        return new Configuration.Builder<AbstractDataSourceBean>(
                uniqueId,
                poolingDataSource,
                Atomikos4PoolAdapter.FACTORY
        )
        .setJmxEnabled(true)
        .setMetricLogReporterMillis(TimeUnit.SECONDS.toMillis(5))
        .build();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public FlexyPoolDataSource dataSource() {
        Configuration<AbstractDataSourceBean> configuration = configuration();
        return new FlexyPoolDataSource<AbstractDataSourceBean>(configuration,
                new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
                new RetryConnectionAcquiringStrategy.Factory(2)
        );
    }
}