分布式事务是指 *** 作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持。
一、使用JOTM例子
(1)、Dao及实现
public interface GenericDao {
public int save(String ds, String sql, Object[] obj) throws Exception
public int findRowCount(String ds, String sql)
} public class GenericDaoImpl implements GenericDao{
private JdbcTemplate jdbcTemplateA
private JdbcTemplate jdbcTemplateB
public void setJdbcTemplateA(JdbcTemplate jdbcTemplate) {
this.jdbcTemplateA = jdbcTemplate
}
public void setJdbcTemplateB(JdbcTemplate jdbcTemplate) {
this.jdbcTemplateB = jdbcTemplate
}
public int save(String ds, String sql, Object[] obj) throws Exception{
if(null == ds || "".equals(ds)) return -1
try{
if(ds.equals("A")){
return this.jdbcTemplateA.update(sql, obj)
}else{
return this.jdbcTemplateB.update(sql, obj)
}
}catch(Exception e){
e.printStackTrace()
throw new Exception("执行" + ds + "数据库时失败!")
}
}
public int findRowCount(String ds, String sql) {
if(null == ds || "".equals(ds)) return -1
if(ds.equals("A")){
return this.jdbcTemplateA.queryForInt(sql)
}else{
return this.jdbcTemplateB.queryForInt(sql)
}
}
}
(2)、Service及实现
public interface UserService {
public void saveUser() throws Exception
} public class UserServiceImpl implements UserService{
private GenericDao genericDao
public void setGenericDao(GenericDao genericDao) {
this.genericDao = genericDao
}
public void saveUser() throws Exception {
String userName = "user_" + Math.round(Math.random()*10000)
System.out.println(userName)
StringBuilder sql = new StringBuilder()
sql.append(" insert into t_user(username, gender) values(?,?) ")
Object[] objs = new Object[]{userName,"1"}
genericDao.save("A", sql.toString(), objs)
sql.delete(0, sql.length())
sql.append(" insert into t_user(name, sex) values(?,?) ")
objs = new Object[]{userName,"男的"}//值超出范围
genericDao.save("B", sql.toString(), objs)
}
}
(3)、applicationContext-jotm.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">
<description>springJTA</description>
<!--指定Spring配置中用到的属性文件-->
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- JOTM实例 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean">
<property name="defaultTimeout" value="500000"/>
</bean>
<!-- JTA事务管理器 -->
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<!-- 数据源A -->
<bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
</property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 数据源B -->
<bean id="dataSourceB" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="${jdbc2.driver}"/>
<property name="url" value="${jdbc2.url}"/>
</bean>
</property>
<property name="user" value="${jdbc2.username}"/>
<property name="password" value="${jdbc2.password}"/>
</bean>
<bean id = "jdbcTemplateA"
class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSourceA"/>
</bean>
<bean id = "jdbcTemplateB"
class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSourceB"/>
</bean>
<!-- 事务切面配置 -->
<aop:config>
<aop:pointcut id="pointCut"
expression="execution(* com.logcd.service..*.*(..))"/><!-- 包及其子包下的所有方法 -->
<aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* *..common.service..*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- 通知配置 -->
<tx:advice id="txAdvice" transaction-manager="jtaTransactionManager">
<tx:attributes>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="find*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<bean id="genericDao"
class="com.logcd.dao.impl.GenericDaoImpl" autowire="byName">
</bean>
<bean id="userService"
class="com.logcd.service.impl.UserServiceImpl" autowire="byName">
</bean>
</beans>
(4)、测试
public class TestUserService{
private static UserService userService
@BeforeClass
public static void init(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-jotm.xml")
userService = (UserService)app.getBean("userService")
}
@Test
public void save(){
System.out.println("begin...")
try{
userService.saveUser()
}catch(Exception e){
System.out.println(e.getMessage())
}
System.out.println("finish...")
}
}
不支持。主要原因是分布式计算,其实是不同服务器起不同的进程在运行。那么事务就无从谈起了。 不过如果满足两个条件,1 每个进程独立起事务 2 进程访问的是传统数据库。那么hibernate还是可以使用的。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)