applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:component-scan base-package="com.ajita.*" /> <tx:annotation-driven /> <tx:jta-transaction-manager /> <bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>DataSourceA</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaProperties"> <props> <prop key="databaseName">test</prop> <prop key="serverName">127.0.0.1</prop> <prop key="port">3306</prop> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="url">jdbc:mysql://127.0.0.1:3306/test</prop> </props> </property> <property name="minPoolSize"> <value>1</value> </property> </bean> <bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>DataSourceB</value> </property> <property name="xaDataSourceClassName"> <value>net.sourceforge.jtds.jdbcx.JtdsDataSource</value> </property> <property name="xaProperties"> <props> <prop key="databaseName">SimpleDB</prop> <prop key="serverName">127.0.0.1</prop> <prop key="portNumber">1434</prop> <prop key="user">sa</prop> <prop key="password">123456</prop> </props> </property> <property name="minPoolSize"> <value>1</value> </property> </bean> <bean id="entityManagerFactoryA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation"> <value>classpath*:persistence.xml</value> </property> <property name="persistenceUnitName" value="PersistenceUnitA" /> <property name="dataSource" ref="dataSourceA" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> </bean> </property> </bean> <bean id="entityManagerFactoryB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation"> <value>classpath*:persistence.xml</value> </property> <property name="persistenceUnitName" value="PersistenceUnitB" /> <property name="dataSource" ref="dataSourceB" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" /> </bean> </property> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="false" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="atomikosTransactionManager,atomikosUserTransaction"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> </beans>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="PersistenceUnitA" transaction-type="JTA"> <class>com.ajita.jta.entity.MySqlUser</class> <properties> <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" /> <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" /> </properties> </persistence-unit> <persistence-unit name="PersistenceUnitB" transaction-type="JTA"> <class>com.ajita.jta.entity.SQLServerUser</class> <properties> <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" /> <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" /> </properties> </persistence-unit> </persistence>
Main.java
package com.ajita.jta; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.ajita.jta.service.UserService; public class Main { public static void main(String[] args) { try { ApplicationContext ctx = new FileSystemXmlApplicationContext( "classpath:applicationContext.xml"); UserService userService = ctx.getBean("userService", UserService.class); userService.saveUser("hwwang2222222222222", "123"); } catch (Exception ex) { ex.printStackTrace(); } } }
UserService.java
package com.ajita.jta.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.ajita.jta.dao.MySqlUserDao; import com.ajita.jta.dao.SQLServerUserDao; import com.ajita.jta.entity.MySqlUser; import com.ajita.jta.entity.SQLServerUser; @Service public class UserService { @Autowired private MySqlUserDao mysqlJpaDao; @Autowired private SQLServerUserDao sqlserverJpaDao; @Transactional public boolean saveUser(String username,String password){ MySqlUser user1=new MySqlUser(); user1.setUsername(username); user1.setPassword(password); mysqlJpaDao.save(user1); SQLServerUser user2=new SQLServerUser(); user2.setUsername(username); sqlserverJpaDao.save(user2); return true; } }
SQLServerUserDao.java
package com.ajita.jta.dao; import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; @Repository("sqlserverJpaDao") public class SQLServerUserDao { @PersistenceContext(unitName = "PersistenceUnitB") private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } public void save(Object entity) { entityManager.persist(entity); } public void update(Object entity) { entityManager.merge(entity); } public <T> void saveAll(Collection<T> entities) { for (T entity : entities) { save(entity); } } public void delete(Object entity) { entityManager.remove(entity); } public <T> void deleteAll(Collection<T> entities) { for (T entity : entities) { delete(entity); } } }
MySqlUserDao.java
package com.ajita.jta.dao; import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; @Repository("mysqlJpaDao") public class MySqlUserDao { @PersistenceContext(unitName = "PersistenceUnitA") private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } public void save(Object entity) { entityManager.persist(entity); } public void update(Object entity) { entityManager.merge(entity); } public <T> void saveAll(Collection<T> entities) { for (T entity : entities) { save(entity); } } public void delete(Object entity) { entityManager.remove(entity); } public <T> void deleteAll(Collection<T> entities) { for (T entity : entities) { delete(entity); } } }
SQLServerUser.java
package com.ajita.jta.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "UserSyn") public class SQLServerUser { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "Name") private String username; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
MySqlUser.java
package com.ajita.jta.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "user_table") public class MySqlUser { @Id @Column(name="ID") @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name="UserName") private String username; @Column(name="Password") private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }