一,当有记录需要操作两个以上数据库时,这时就会引发事务问题,,,jndi能解决此类问题
context.xml
<Context path="/spring-all" docBase="spring-all" debug="5" reloadable="true" crossContext="true"> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="50" maxWait="10000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/orange1" username="root" password="kgddxsksk" testOnBorrow="true" testWhileIdle="true"/> <Resource name="jdbc/sqlserver" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="50" maxWait="10000" driverClassName="net.sourceforge.jtds.jdbc.Driver" url="jdbc:jtds:sqlserver://192.168.7.83:1433/test" username="sa" password="kgddxsksk" testOnBorrow="true" testWhileIdle="true"/> </Context>
二,spring中的事务配置
<?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" xmlns:jee="http://www.springframework.org/schema/jee" 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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <!-- <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3306/orange1</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>kgddxsksk</value> </property> </bean> --> <jee:jndi-lookup id="dataSource1" jndi-name="java:/comp/env/jdbc/mysql"> </jee:jndi-lookup> <jee:jndi-lookup id="dataSource2" jndi-name="java:/comp/env/jdbc/sqlserver"> </jee:jndi-lookup> <bean id="jotm" class="com.spring.web.controller.JotmFactoryBean" /> <!-- 当使用jta时,说明使用隔离级别 --> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="allowCustomIsolationLevels" value="true" /> <property name="userTransaction"> <ref local="jotm" /> </property> </bean> <!-- "person" table in mysql and "user" table in sqlserver --> <bean id="person" class="com.spring.web.model.Person"></bean> <bean id="personDao" class="com.spring.web.dao.PersonDao"> <property name="dataSource" ref="dataSource1"></property> </bean> <bean id="user" class="com.spring.web.model.User"></bean> <bean id="userDao" class="com.spring.web.dao.UserDao"> <property name="dataSource" ref="dataSource2"></property> </bean> <bean id="userService" class="com.spring.web.services.UserService"> <property name="userDao" ref="userDao"></property> <property name="personDao" ref="personDao"></property> </bean> <!-- 事务配置 --> <!-- 非检查型(RuntimeException)默认才会回滚 --> <!-- 对txManager进行了事务控制,,,所以在代码中使用持久化api时,也要是定制的,如 JdbcTemplate、HibernateTemplate和JdoTemplate,,,也可用相应的 DataSourceUtils(针对JDBC),SessionFactoryUtils(针对Hibernate),PersistenceManagerFactoryUtils(针对JDO) 因为这些类都会引用manager中的事务 --> <tx:annotation-driven transaction-manager="txManager" /> <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes > <!-- propagation 只有在嵌套事务(service调用了service里面的方法)存在时,几种机制才有意义 --> <tx:method name="insert*" propagation="REQUIRED" isolation="REPEATABLE_READ"/> </tx:attributes> </tx:advice> <!-- ensure that the above transactional advice runs for any execution of an operation defined by the Service interface --> <aop:config> <aop:pointcut id="daoOperation" expression="execution (* com.spring.web.services.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation" /> </aop:config> </beans>
JotmFactoryBean在新版本中已实移除,需自己加入才可以.
public class JotmFactoryBean implements FactoryBean, DisposableBean { private Current jotmCurrent; private Jotm jotm; public JotmFactoryBean() throws NamingException { // Check for already active JOTM instance. this.jotmCurrent = Current.getCurrent(); // If none found, create new local JOTM instance. if (this.jotmCurrent == null) { // Only for use within the current Spring context: // local, not bound to registry. this.jotm = new Jotm(true, false); this.jotmCurrent = Current.getCurrent(); } } /** * Set the default transaction timeout for the JOTM instance. * <p>Should only be called for a local JOTM instance, * not when accessing an existing (shared) JOTM instance. */ public void setDefaultTimeout(int defaultTimeout) { this.jotmCurrent.setDefaultTimeout(defaultTimeout); } /** * Return the JOTM instance created by this factory bean, if any. * Will be <code>null</code> if an already active JOTM instance is used. * <p>Application code should never need to access this. */ public Jotm getJotm() { return this.jotm; } public Object getObject() { return this.jotmCurrent; } public Class getObjectType() { return this.jotmCurrent.getClass(); } public boolean isSingleton() { return true; } /** * Stop the local JOTM instance, if created by this FactoryBean. */ public void destroy() { if (this.jotm != null) { this.jotm.stop(); } } }
还得加入jotm中相应的一些包..http://androidguy.blog.51cto.com/974126/216454
carol.jar
carol-iiop-delegate.jar
carol-interceptors.jar
howl.jar
jotm.jar
xapool.jar