`
cjblog
  • 浏览: 68619 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

spring集成hibernate事物JTA

阅读更多

环境介绍:atomikos3.8,spring3,hibernate3.6.10

jdbc.properties配置:

 

#first
first.xaDataSourceClassName=oracle.jdbc.xa.client.OracleXADataSource
first.jdbc.driver=oracle.jdbc.driver.OracleDriver
first.jdbc.url=jdbc:oracle:thin:@192.168.0.2:1521:HYXT
first.jdbc.username=***
first.jdbc.password=***

second.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
second.jdbc.driver=com.mysql.jdbc.Driver
second.jdbc.url=jdbc:mysql://192.168.0.37:52/bs?useUnicode=true&characterEncoding=utf8&pinGlobalTxToPhysicalConnection=true
second.jdbc.username=***
second.jdbc.password=***

third.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
third.jdbc.driver=com.mysql.jdbc.Driver
third.jdbc.url=jdbc:mysql://192.168.0.37:52/third?useUnicode=true&characterEncoding=utf8&pinGlobalTxToPhysicalConnection=true
third.jdbc.username=***
third.jdbc.password=***

persistence.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
	version="2.0">
	<persistence-unit name="first" transaction-type="JTA">
	</persistence-unit>
	<persistence-unit name="second" transaction-type="JTA">
	</persistence-unit>
</persistence>

  

 

atomikos-hibernate3.xml配置:

 

 

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
		   http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:annotation-config />
	<aop:aspectj-autoproxy />
	<tx:annotation-driven />
	<context:component-scan base-package="org.lmc.platform.jta" />

	<context:property-placeholder
		ignore-resource-not-found="true" location="classpath*:/jdbc.properties" />

	<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
		init-method="init" destroy-method="close">
		<property name="forceShutdown" value="true" />
	</bean>
	<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
		<property name="transactionTimeout" value="300" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager" ref="atomikosTransactionManager" />
		<property name="userTransaction" ref="atomikosUserTransaction" />
		<property name="allowCustomIsolationLevels" value="true" />
	</bean>

	<bean id="firstDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="firstDS" />
		<property name="xaDataSourceClassName" value="${first.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${first.jdbc.username}</prop>
				<prop key="password">${first.jdbc.password}</prop>
				<prop key="URL">${first.jdbc.url}</prop>
			</props>
		</property>
		<property name="poolSize" value="10" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="10" />
		<property name="testQuery" value="select 1 from dual" />
	</bean>
	
	<bean name="hibernateProperties"  
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
        <property name="properties">  
            <props>  
                <prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.current_session_context_class">jta</prop>
				<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            </props>  
        </property>  
    </bean> 
	
	<bean id="firstEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="persistenceUnitName" value="first" />
		<property name="dataSource" ref="firstDataSource" />
		 <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true"/>  
            </bean>
        </property>
		<property name="jpaProperties" ref="hibernateProperties"/>
	</bean>

	<bean id="secondDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="secondDS" />
		<property name="xaDataSourceClassName" value="${second.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${second.jdbc.username}</prop>
				<prop key="password">${second.jdbc.password}</prop>
				<prop key="URL">${second.jdbc.url}</prop>
			</props>
		</property>
		<property name="poolSize" value="2" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="5" />
		<property name="testQuery" value="select 1" />
	</bean>

	<bean id="secondEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="persistenceUnitName" value="second" />
		<property name="dataSource" ref="secondDataSource" />
		<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true"/>  
            </bean>
        </property>
		<property name="jpaProperties" ref="hibernateProperties"/>
	</bean>

	<bean id="thirdDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="thirdDS" />
		<property name="xaDataSourceClassName" value="${third.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${third.jdbc.username}</prop>
				<prop key="password">${third.jdbc.password}</prop>
				<prop key="URL">${third.jdbc.url}</prop>
			</props>
		</property>
		<property name="poolSize" value="2" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="5" />
		<property name="testQuery" value="select 1" />
	</bean>
	
	<bean id="thirdSessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="thirdDataSource" />
		<property name="packagesToScan">
			<list>
				<value>org.**.entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>


</beans>

 使用的时候这样写就ok了:

 

@PersistenceContext(unitName = "first") 
    private EntityManager em ;
	
	@Autowired
	private SessionFactory thirdSessionFactory;

 对指定的业务方法加上@Transactional就可以实现事物的管理。

下面是hibernate4的写法atomikos-hibernate4.xml:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
		   http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:annotation-config />
	<aop:aspectj-autoproxy />
	<tx:annotation-driven />
	<context:component-scan base-package="org.lmc.platform.jta" />

	<context:property-placeholder
		ignore-resource-not-found="true" location="classpath*:/jdbc.properties" />

	<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
		init-method="init" destroy-method="close">
		<property name="forceShutdown" value="true" />
	</bean>
	<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
		<property name="transactionTimeout" value="300" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager" ref="atomikosTransactionManager" />
		<property name="userTransaction" ref="atomikosUserTransaction" />
		<property name="allowCustomIsolationLevels" value="true" />
	</bean>

	<bean id="atomikosJtaPlatform" class="org.lmc.platform.jta.AtomikosJtaPlatform">
		<property name="transactionManager" ref="atomikosTransactionManager" />
		<property name="userTransaction" ref="atomikosUserTransaction" />
	</bean>

	<bean id="firstDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="firstDS" />
		<property name="xaDataSourceClassName" value="${first.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${first.jdbc.username}</prop>
				<prop key="password">${first.jdbc.password}</prop>
				<prop key="URL">${first.jdbc.url}</prop>
			</props>
		</property>
		<property name="maxPoolSize" value="10" />
		<property name="testQuery" value="select 1 from dual" />
	</bean>
	
	<bean name="hibernateProperties"  
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
        <property name="properties">  
            <props>  
          		<prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>
				<prop key="hibernate.transaction.jta.platform">org.lmc.platform.jta.AtomikosJtaPlatform</prop>
				<prop key="hibernate.current_session_context_class">jta</prop>
				<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            </props>  
        </property>  
    </bean> 
	
	<bean id="firstEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="persistenceUnitName" value="first" />
		<property name="dataSource" ref="firstDataSource" />
		 <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true"/>  
            </bean>
        </property>
		<property name="jpaProperties" ref="hibernateProperties"/>
	</bean>

	<bean id="secondDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="secondDS" />
		<property name="xaDataSourceClassName" value="${second.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${second.jdbc.username}</prop>
				<prop key="password">${second.jdbc.password}</prop>
				<prop key="URL">${second.jdbc.url}</prop>
			</props>
		</property>
		<property name="maxPoolSize" value="10" />
		<property name="testQuery" value="select 1 from dual" />
	</bean>

	<bean id="secondEntityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="persistenceUnitName" value="second" />
		<property name="dataSource" ref="secondDataSource" />
		<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true"/>  
            </bean>
        </property>
		<property name="jpaProperties" ref="hibernateProperties"/>
	</bean>

	<bean id="thirdDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="thirdDS" />
		<property name="xaDataSourceClassName" value="${third.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${third.jdbc.username}</prop>
				<prop key="password">${third.jdbc.password}</prop>
				<prop key="URL">${third.jdbc.url}</prop>
			</props>
		</property>
		<property name="maxPoolSize" value="10" />
		<property name="testQuery" value="select 1 from dual" />
	</bean>
	
	<bean id="thirdSessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="thirdDataSource" />
		<property name="packagesToScan">
			<list>
				<value>org.**.entity</value>
			</list>
		</property>
		<property name="hibernateProperties" ref="hibernateProperties"/>
	</bean>


</beans>

hibernate4多了一个类AtomikosJtaPlatform:

 

package org.lmc.platform.jta;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;

public class AtomikosJtaPlatform extends AbstractJtaPlatform {
	private static final long serialVersionUID = 4896569440655581002L;
	private static TransactionManager transactionManager;
	private static UserTransaction userTransaction;
	
	@Override
	protected TransactionManager locateTransactionManager() {
		return AtomikosJtaPlatform.transactionManager;
	}

	@Override
	protected UserTransaction locateUserTransaction() {
		return AtomikosJtaPlatform.userTransaction;
	}

	public void setTransactionManager(TransactionManager transactionManager) {
		AtomikosJtaPlatform.transactionManager = transactionManager;
	}

	public void setUserTransaction(UserTransaction userTransaction) {
		AtomikosJtaPlatform.userTransaction = userTransaction;
	}

	
}

 

 

oracle记着要授权,并且使用11的驱动包,jdbc注意连接的配置,

以sysdba登录数据库,给相应地用户赋予以下权限:

  • grant select on sys.dba_pending_transactions to <当前数据库用户>;
  • grant select on sys.pending_trans$ to <当前数据库用户>;
  • grant select on sys.dba_2pc_pending to <当前数据库用户>;
  • grant execute on sys.dbms_system to <当前数据库用户>;

mysql注意使用5以上,加上pinGlobalTxToPhysicalConnection参数。

 

 

 

0
1
分享到:
评论
3 楼 lingfang1986 2014-02-28  
你好,我看了你的博客,我这完全从你那上面copy下来的代码,一开始是提示找不到META-INF/persistence.xml这个文件,后来我把你的代码classpath:META-INF/persistence.xml改成classpath*:META-INF/persistence.xml就没这个提示了,但再重新启动就提示No persistence unit with name 'second' found,能帮我看一下是啥问题不?
2 楼 cjblog 2014-02-28  
lingfang1986 写道
META-INF/persistence.xml和web.xml的代码能贴出来不?我按你这个配置走不通呀,hibernate4的,能加扣扣聊一下不?扣扣407073846

persistence.xml已经贴出来了。web.xml没有
1 楼 lingfang1986 2014-02-28  
META-INF/persistence.xml和web.xml的代码能贴出来不?我按你这个配置走不通呀,hibernate4的,能加扣扣聊一下不?扣扣407073846

相关推荐

    spring+hibernate+jta demo

    《Spring、Hibernate与JTA在MyEclipse中的整合实践》 在现代企业级应用开发中,Spring、Hibernate和Java Transaction API(JTA)是常见的技术组合。Spring作为轻量级的IoC(Inversion of Control)和AOP(Aspect ...

    第二部分spring+hibernate+jta 分布式事务Demo

    在本示例中,`myProject`是一个包含源码的项目,用于演示如何在Spring、Hibernate和JTA集成环境下实现分布式事务。通常,这个项目会包含以下关键组成部分: - **配置文件**:`applicationContext.xml`是Spring的...

    JTA-demo:spring+hibernate+jta+mysql

    标题“JTA-demo:spring+hibernate+jta+mysql”揭示了一个集成示例,它展示了如何在Java应用中使用Spring框架、Hibernate ORM工具以及Java Transaction API(JTA)来管理事务,同时与MySQL数据库进行交互。...

    ssh集成jar包,支持spring集成Hibernate,spring集成struts2等

    - Spring还提供了对其他库的集成,如JDBC、JMS、JTA等,以及对其他框架如Hibernate、Struts2的整合支持。 2. **Hibernate框架**: - Hibernate是一个对象关系映射(Object-Relational Mapping,ORM)框架,它简化...

    spring集成hibernate所需jar包

    将Spring与Hibernate集成,可以充分利用两者的优点,实现高效且灵活的后端数据处理。 在"spring集成hibernate所需jar包"中,通常包含以下关键的库文件: 1. **Spring Framework**:这是Spring的核心组件,包括`...

    spring 结合druid和hibernate使用jta配置分布式事务

    `Atomikos` 是一个开源的JTA实现,它提供了对分布式事务的支持,可以与Spring集成,处理多数据源的事务管理。 在Spring中配置Druid和Hibernate结合Atomikos进行分布式事务管理的步骤如下: 1. **引入依赖**:在...

    jta hibernate spring 集成 代码和说明

    在Java世界中,集成JTA(Java Transaction API)、Hibernate ORM框架和Spring框架是构建企业级分布式应用程序的关键步骤。这三个组件的结合使得应用能够处理复杂的事务管理、持久化数据以及依赖注入,从而提供强大的...

    Jboss4.2.2+Spring2.5.6+hibernate+JTA事务的实现

    "Jboss4.2.2+Spring2.5.6+Hibernate+JTA事务的实现"就是一个典型的例子,它涉及到四个关键的技术栈,即JBoss Application Server 4.2.2、Spring 2.5.6、Hibernate ORM以及Java Transaction API(JTA)。这些技术的...

    spring和hibernate jar包

    - **Session和Transaction管理**:Hibernate提供Session接口处理数据库会话,同时支持JTA和JTASessionContext,便于事务管理。 - **HQL和QBC**:Hibernate Query Language (HQL) 是面向对象的查询语言,类似SQL。...

    整合Spring + hibernate所需的jar包

    这些jar文件通常包括Spring的核心模块、数据访问/集成模块以及Hibernate的核心库和其他相关依赖。 Spring框架的jar文件可能包含以下关键组件: 1. spring-context:提供核心容器功能,包括依赖注入(DI)和bean管理...

    Spring源代码解析(八):Spring驱动Hibernate的实现.doc

    总的来说,Spring通过`LocalSessionFactoryBean`实现对Hibernate的驱动,它将数据源、事务管理和配置信息集成在一起,创建出适应Spring管理的`SessionFactory`。这种集成方式使得开发者无需过多关注底层细节,可以...

    spring+hibernate+struts架包

    5. **集成其他框架**:Spring能与Hibernate、Struts等框架无缝集成,形成强大的企业级应用解决方案。 Hibernate框架的关键优势有: 1. **ORM映射**:Hibernate通过XML映射文件或注解将Java对象与数据库表进行映射...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    spring+hibernate+atomikos多数据源

    Spring、Hibernate和Atomikos的组合就是一种强大的解决方案,它们可以协同工作以支持多数据源的分布式事务处理。接下来,我们将深入探讨这些技术以及如何配置它们来实现多数据源。 1. **Spring**: Spring是一个...

    struts1+spring+hibernate所需jar包

    Struts1、Spring和Hibernate是Java Web开发中的三个重要框架,它们各自负责应用程序的不同层面:Struts1专注于表现层管理,Spring提供了强大的依赖注入和事务管理功能,而Hibernate则是持久层解决方案,简化了数据库...

    spring+hibernate代码实例集

    2.2 Hibernate默认支持JTA(Java Transaction API)事务,Spring可以配置JtaTransactionManager,配合Hibernate实现全局事务管理。在测试环境中,通常使用PlatformTransactionManager,如...

Global site tag (gtag.js) - Google Analytics