web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- spring listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-jta.xml</param-value>
</context-param>
<!-- spring log4j -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<!-- struts filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="GBK" />
<constant name="struts.objectFactory" value="spring" />
<package name="useradmin" extends="struts-default">
<action name="AddUser" class="addUserAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
</property>
<property name="username">
<value>estarcom</value>
</property>
<property name="password">
<value>estarcom</value>
</property>
</bean>
<!-- hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>test/entity/User.hbm.xml</value>
<value>test/entity/UserProfile.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- dao bean -->
<bean id="userDao" class="test.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="profileDao" class="test.dao.impl.UserProfileDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- service bean -->
<bean id="userService" class="test.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
<property name="profileDao">
<ref local="profileDao" />
</property>
</bean>
<!--struts action bean -->
<bean id="addUserAction" class="test.action.AddUserAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>
<!-- transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- transaction control with aop -->
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* test.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
如果要使用jotm对多数据源进行JTA事务管理,配置文件如下:applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction">
<ref local="jotm" />
</property>
</bean>
<bean id="oracleDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean id="innerOracleDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager">
<ref local="jotm" />
</property>
<property name="driverName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
</property>
<property name="user">
<value>estarcom</value>
</property>
<property name="password">
<value>estarcom</value>
</property>
</bean>
</property>
<property name="maxSize">
<value>5</value>
</property>
<property name="minSize">
<value>2</value>
</property>
<property name="user">
<value>estarcom</value>
</property>
<property name="password">
<value>estarcom</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="oracleDataSource" />
<property name="mappingResources">
<list>
<value>test/entity/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="jtaTransactionManager">
<ref bean="jotm" />
</property>
</bean>
<bean id="oracleDataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean id="innerOracleDataSource2" class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager">
<ref local="jotm" />
</property>
<property name="driverName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
</property>
<property name="user">
<value>shmobile</value>
</property>
<property name="password">
<value>shmobile</value>
</property>
</bean>
</property>
<property name="maxSize">
<value>5</value>
</property>
<property name="minSize">
<value>2</value>
</property>
<property name="user">
<value>shmobile</value>
</property>
<property name="password">
<value>shmobile</value>
</property>
</bean>
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="oracleDataSource2" />
<property name="mappingResources">
<list>
<value>test/entity/UserProfile.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="jtaTransactionManager">
<ref bean="jotm" />
</property>
</bean>
<!-- dao bean -->
<bean id="userDao" class="test.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="profileDao" class="test.dao.impl.UserProfileDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory2" />
</property>
</bean>
<!-- service bean -->
<bean id="userService" class="test.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
<property name="profileDao">
<ref local="profileDao" />
</property>
</bean>
<!--struts action bean -->
<bean id="addUserAction" class="test.action.AddUserAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>
<!-- transaction control with aop -->
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* test.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>