Hibernate 事务处理和spring中配置事务
2012-12-25 00:16 eoeAndroid社区 阅读(348) 评论(0) 收藏 举报1.非集成spring事务管理
事务是指由一个或者多个SQL语句组成的工作单元,这个单元中SQL语句只要有一个SQL语句执行失败,就会撤销整个工作单元。
事务的成功取决于工作单元的所有SQL语句都执行成功,它必须具备ACID特征,ACID是Atomic(原子性)、Consistency(一致性)、Isolation(隔离性)和持久性(Durability),它们的含义是:
(1)
(2)
(3)
(4)
2声明事务边界
在mysql.exe程序中声明事务
(1)
(2)
1.
2.
3.
如以下的银行转帐事务
begin transaction
set @errorSum=0
update bank set currentMoney=currentMoney-1000 where customerName=’张三’
set @errorSum=@errorSum+@@error--@@error是系统的全局变量.
update bank set currentMoney=currentMoney+1000 where customerName=’李四’
set @errorSum=@errorSum+@@error
if @errorSum<>0
else
通过JDBC API声明事务边界
(1)
(2)
(3)
- try{
- con.setAutoCommit(false);//设置为手工提交模式
- stmt=con.createStatement();
- stmt.executeUpdate(“update ACCOUNTS set BALANCE=1000-100 where ID=1”);
- stmt.executeUpdate(update ACCOUNTS set BALANCE=0+100 where ID=2”)
- con.commit();//提交事务
- }catch(SQLException e){
- con.rollback();//不成功就撤销事务.这语句也要捕获异常,代码略
- }finally{
- stmt.close();
- con.close();//.这语句也要捕获异常,代码
- }
通过Hibernate API声明事务边界
(1)
(2)
(3)
- Session session=sessionFactory.openSession();
- Transaction tx;
- try{
- tx=session.beginTransaction();//开始一个事务
- ….//执行一些操作
- tx.commit();//提交事务
- }catch(Exception e){
- tx.rollback();//撤销事务。这个语句也要捕获异常,代码略
- }finally{
- session.close();//撤销事务。这个语句也要捕获异常,代码略
- }
- try{
- tx1=session.beginTransaction();
- ….//执行一些操作
- tx1.commit();//提交事务
- session.desconnect();//释放数据连接
- ….//执行一些操作,这些操作不属于任何事务
- session.reconnnect();//重新获得数据库连接
- tx2=session.beginTranction();//开始第二个事务
- ….// 执行一些操作
- tx2.commit();//提交第二个事务
- }catch(Exception e){
- if(tx1!=null)tx1.rollback();
- if(tx2!=null)tx2.rollback();
- }finally{
- session.close();
- }
2.集成spring事务管理
<?xml version="1.0" encoding="UTF-8"?> <!-- 指定Spring配置文件的Schema信息 --> <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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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" default-autowire="byName"> <!-- 定义数据源Bean,使用C3P0数据源实现 --> <!-- 设置连接数据库的驱动、URL、用户名、密码 连接池最大连接数、最小连接数、初始连接数等参数 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost:3306/sshweb?useUnicode=true&characterEncoding=utf-8" p:user="root" p:password="123456" p:maxPoolSize="40" p:minPoolSize="1" p:initialPoolSize="1" p:maxIdleTime="20"/> <!-- 定义Hibernate的SessionFactory --> <!-- 依赖注入数据源,注入正是上面定义的dataSource --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <!-- mappingResouces属性用来列出全部映射文件 --> <!--<property name="mappingResources"> <list> <value>com/model/Novel.hbm.xml</value> <value>com/model/NovelType.hbm.xml</value> </list> </property>--> <property name="mappingLocations"> <list> <value>classpath:com/model/mapping/*.hbm.xml</value> </list> </property> <!-- 定义Hibernate的SessionFactory的属性 --> <property name="hibernateProperties"> <!-- 指定数据库方言、是否自动建表 是否生成SQL语句等 --> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.connection.characterEncoding=UTF-8 <!-- hibernate.format_sql=true--> <!-- #开启二级缓存--> <!-- hibernate.cache.use_second_level_cache=true--> <!-- #设置二级缓存的提供者--> <!-- hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider--> </value> </property> </bean> <!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 --> <!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现--> <!-- 并注入SessionFactory的引用 --> <bean id="transactionManager" class= "org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/> <!-- 配置事务增强处理Bean,指定事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 用于配置详细的事务语义 --> <tx:attributes> <!-- 所有以'get'开头的方法是read-only的 --> <tx:method name="get*" read-only="true"/> <!-- 其他方法使用默认的事务设置 --> <tx:method name="*"/> <!--不需要事务管理的-->
<tx:method name="get*" read-only="false" propagation="NOT-SUPPORTED"/>
</tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <!-- 配置一个切入点,匹配empManager和mgrManager 两个Bean的所有方法的执行 --> <aop:pointcut id="pointcut" expression="execution(* com.service.*.*(..))" /> <!-- 指定在leePointcut切入点应用txAdvice事务增强处理 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 装配HibernateTemplate实例 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <constructor-arg ref="sessionFactory"/> </bean> --> </beans>
浙公网安备 33010602011771号