Search code examples
javaspringbitronix

Integrating Bitronix with Jtds driver


I am trying to execute a PoolingDataSource with the Jtds driver for SQL Server and Sybase connections.

protected DataSource dataSource() throws Exception {
    // Create pool datasource
    final PoolingDataSource poolingDataSource = new PoolingDataSource();
    poolingDataSource.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
    poolingDataSource.setMaxPoolSize(20);
    poolingDataSource.setUniqueName(getDataSourceProperty().getUserName());
    poolingDataSource.setAllowLocalTransactions(true);
    poolingDataSource.setAutomaticEnlistingEnabled(true);
    poolingDataSource.setShareTransactionConnections(false);

    final Properties properties = new Properties();
    properties.put("driverClassName", getDataSourceProperty().getDriverClassName());
    properties.put("url", getDataSourceProperty().getUrl());
    properties.put("user", getDataSourceProperty().getUserName());
    properties.put("password", DataSourceSecureIdentity.decode(getDataSourceProperty().getPassword()));

    poolingDataSource.setDriverProperties(properties);

    return poolingDataSource;
}

Once the server is started it shows:

Caused by: bitronix.tm.internal.BitronixSystemException: cannot enlist more than one non-XA resource, tried enlisting an XAResourceHolderState with uniqueName=xxx XAResource=a JDBC LrcXAResource in state NO_TX with XID null, already enlisted: an XAResourceHolderState with uniqueName=xxx2 XAResource=a JDBC LrcXAResource in state STARTED (started) with XID a Bitronix XID [7465737465000000001757A2D000000005 : 7465737465000000001757A70A00000006]
    at bitronix.tm.internal.XAResourceManager.enlist(XAResourceManager.java:107) ~[btm-2.1.4.jar:2.1.4]
    at bitronix.tm.BitronixTransaction.enlistResource(BitronixTransaction.java:130) ~[btm-2.1.4.jar:2.1.4]
    at bitronix.tm.resource.common.TransactionContextHelper.enlistInCurrentTransaction(TransactionContextHelper.java:69) ~[btm-2.1.4.jar:2.1.4]
    at bitronix.tm.resource.jdbc.JdbcConnectionHandle.enlistResource(JdbcConnectionHandle.java:85) ~[btm-2.1.4.jar:2.1.4]
    ... 93 common frames omitted
org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is bitronix.tm.internal.BitronixRollbackException: transaction was marked as rollback only and has been rolled back
    at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1024)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

I read I can only have one Non-XA datasource, but if the LrcXADataSource class is wrapping the datasource why does it happen?

Is there any way to inform Jtds Driver about XA option?


Solution

  • I found out that emulating a XA DataSource via LrcXADataSource and inserting into a XA pool via PoolingDataSourceBean I can have multiple Non-XA datasources working on Bitronix.

    protected DataSource dataSource() throws Exception {
        // Create XA datasource
        final LrcXADataSource lrcXADataSource = new LrcXADataSource();
        lrcXADataSource.setDriverClassName(getDataSourceProperty().getDriverClassName());
        lrcXADataSource.setUrl(getDataSourceProperty().getUrl());
        lrcXADataSource.setUser(getDataSourceProperty().getUserName());
        lrcXADataSource.setPassword(DataSourceSecureIdentity.decode(getDataSourceProperty().getPassword()));
    
        // Create a pool of XA datasource
        final PoolingDataSourceBean poolingDataSourceBean = new PoolingDataSourceBean();
        poolingDataSourceBean.setDataSource(lrcXADataSource);
        poolingDataSourceBean.setMaxPoolSize(getDataSourceProperty().getMaxPoolSize());
        poolingDataSourceBean.setMinPoolSize(getDataSourceProperty().getMinPoolSize());
        poolingDataSourceBean.setTestQuery(getDataSourceProperty().getValidationQuery());
    
        return poolingDataSourceBean;
    }