Skip to content

Instantly share code, notes, and snippets.

@teabot
Last active September 15, 2017 05:48
Show Gist options
  • Save teabot/4555886 to your computer and use it in GitHub Desktop.
Save teabot/4555886 to your computer and use it in GitHub Desktop.
Example distributed XA transaction configuration for: Bitronix BTM, Spring, Hibernate, IBatis, Last Resource Commit
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices">
<property name="serverId" value="${node.name}-btx-server" />
<property name="logPart1Filename" value="${transaction.manager.logfile1:/var/run/myproject/${node.name}-btm1.tlog}" />
<property name="logPart2Filename" value="${transaction.manager.logfile2:/var/run/myproject/${node.name}-btm2.tlog}" />
</bean>
<!-- create BTM transaction manager -->
<bean id="bitronixTransactionManager" factory-method="getTransactionManager" class="bitronix.tm.TransactionManagerServices"
depends-on="btmConfig" destroy-method="shutdown" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" primary="true">
<property name="transactionManager" ref="bitronixTransactionManager" />
<property name="userTransaction" ref="bitronixTransactionManager" />
</bean>
<!-- Our LocalJtaTransactionFactory allows us to avoid JNDI -->
<bean id="jtaTransactionFactory" class="my.project.hibernate.LocalJtaTransactionFactory">
<property name="userTransaction" ref="bitronixTransactionManager" />
</bean>
<bean id="primaryDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init"
destroy-method="close">
<property name="uniqueName" value="primary-postgres" />
<property name="className" value="org.postgresql.xa.PGXADataSource" />
<property name="maxPoolSize" value="${primary.db.pool.size:1}" />
<property name="testQuery" value="SELECT 1" />
<property name="allowLocalTransactions" value="true" />
<property name="driverProperties">
<props>
<prop key="user">${primary.db.user}</prop>
<prop key="password">${primary.db.password}</prop>
<prop key="serverName">${primary.db.host}</prop>
<prop key="portNumber">${primary.db.port}</prop>
<prop key="databaseName">${primary.db.dbname}</prop>
</props>
</property>
</bean>
<bean id="primarySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:/maps/primary-sql-map-config.xml" />
<property name="dataSource" ref="primaryDataSource" />
</bean>
<bean id="secondaryDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init" destroy-method="close"
primary="true">
<property name="uniqueName" value="secondary-postgres" />
<property name="className" value="bitronix.tm.resource.jdbc.lrc.LrcXADataSource" />
<property name="maxPoolSize" value="${secondary.db.pool.size:1}" />
<property name="testQuery" value="SELECT 1" />
<property name="allowLocalTransactions" value="true" />
<property name="driverProperties">
<props>
<prop key="user">${secondary.db.user}</prop>
<prop key="password">${secondary.db.password}</prop>
<prop key="url">jdbc:postgresql://${secondary.db.host}:${secondary.db.port}/${secondary.db.dbname}</prop>
<prop key="driverClassName">org.postgresql.Driver</prop>
</props>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
primary="true" depends-on="jtaTransactionFactory">
<property name="dataSource" ref="secondaryDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto:validate}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.connection.isolation">3</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.BTMTransactionManagerLookup
</prop>
<!-- Our LocalJtaTransactionFactory allows us to avoid JNDI -->
<prop key="hibernate.transaction.factory_class">
my.project.hibernate.LocalJtaTransactionFactory
</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>my.project.model</value>
</list>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment