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(); } } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ajita</groupId> <artifactId>SimpleDemo</artifactId> <version>0.1</version> <repositories> <repository> <id>maven.apache</id> <name>maven.apache</name> <url>http://repo.maven.apache.org/</url> </repository> <repository> <id>maven</id> <name>maven</name> <url>http://repo1.maven.org/maven2/</url> </repository> </repositories> <properties> <spring.version>3.1.0.RELEASE</spring.version> <hibernate.version>4.1.0.Final</hibernate.version> <atomikos.version>3.8.0</atomikos.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> <exclusions> <exclusion> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </exclusion> <exclusion> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.atomikos</groupId> <artifactId>transactions-jta</artifactId> <version>${atomikos.version}</version> </dependency> <dependency> <groupId>com.atomikos</groupId> <artifactId>transactions-jdbc</artifactId> <version>${atomikos.version}</version> </dependency> <dependency> <groupId>com.atomikos</groupId> <artifactId>transactions-hibernate3</artifactId> <version>${atomikos.version}</version> <exclusions> <exclusion> <artifactId>hibernate</artifactId> <groupId>org.hibernate</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <finalName>SimpleDemo</finalName> </build> </project>
<?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: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">myoa</prop> <prop key="serverName">127.0.0.1</prop> <prop key="port">3306</prop> <prop key="user">root</prop> <prop key="password">system</prop> <prop key="url">jdbc:mysql://127.0.0.1:3306/myoa</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>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaProperties"> <props> <prop key="databaseName">myoa2</prop> <prop key="serverName">127.0.0.1</prop> <prop key="port">3306</prop> <prop key="user">root</prop> <prop key="password">system</prop> <prop key="url">jdbc:mysql://127.0.0.1:3306/myoa2</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>
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; } }
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; } }
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; } }
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); } } }
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); } } }
相关推荐
antlr-2.7.7.jar dom4j-1.6.1.jar geronimo-jta_1.1_spec-1.1.1.jar hibernate-commons-annotations-5.0.1.Final.jar hibernate-core-5.0.7.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-2.0.0.Final....
本项目"spring-jta-mybatis"着重讲解了如何在Spring框架中结合JTA(Java Transaction API)和MyBatis实现分布式事务管理。 首先,分布式数据源是在多数据库环境下的一个重要概念。当业务需求扩大,单一数据库无法...
本文档将深入探讨JTA 1.1的核心概念、接口以及在实际应用中的实现。 1. **JTA基本概念** - **事务(Transaction)**:事务是数据库操作的基本单位,它保证了一组操作的原子性、一致性、隔离性和持久性(ACID特性)...
《Spring、Hibernate与JTA在MyEclipse中的整合实践》 在现代企业级应用开发中,Spring、Hibernate和Java Transaction API(JTA)是常见的技术组合。Spring作为轻量级的IoC(Inversion of Control)和AOP(Aspect ...
总的来说,这个JTA-demo是一个学习和实践Java企业级事务管理的好资源,它演示了Spring、Hibernate和JTA在复杂环境中的协同工作,这对于理解和实现大型分布式系统中的事务处理至关重要。通过深入研究这个示例,开发者...
Hibernate 支持JTA(Java Transaction API)和JDBC事务管理,确保数据的一致性和完整性。 8. **实体状态**: Hibernate定义了四种实体状态:瞬时态、持久态、游离态和脱管态,理解这些状态对于正确使用Hibernate...
2. **org.hibernate**:这是核心代码所在的主要包,下辖多个子包,涵盖了Hibernate的所有核心功能,如: - `configuration`:负责配置信息的读取和解析,包括XML配置文件和程序化配置。 - `entitymanager`:实现了...
通过阅读“JAVA JTA jar-doc-spec”中的文档和规范,开发者可以深入理解JTA的工作原理,学习如何在Java应用中实现事务管理,以及如何与其他Java EE组件如JDBC和JMS结合使用,以构建健壮的、高可用的企业级应用。...
Hibernate是Java领域中一款著名...总之,`hibernate-release-5.0.7.Final`版本的jar包集合是Java开发者进行ORM编程的强大工具,涵盖了从数据持久化到事务处理、验证和缓存等多个方面,极大地提升了开发效率和代码质量。
`transactions-hibernate3.jar`和`transactions-hibernate2.jar`可能是旧版本的Atomikos对Hibernate的适配库,用于整合Hibernate与JTA。在现代项目中,我们通常会使用更兼容的版本或Spring Data JPA来配合JTA事务。 ...
自制CHM版的API文档,带索引。 注:如果各位下载后打开或无法显示页面,请在CHM文件右键—属性—解除锁定即可。
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上
jta.jar 各个版本,免费下载 transactions-jta.jar 各个版本,免费下载 ow2-jta-1.1-spec.jar 各个版本,免费下载 JTA规范,当 Hibernate 使用 JTA 的时候需要,不过 App Server 都会带上