日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(转载)spring配置hibernate 事务。

發(fā)布時(shí)間:2024/7/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转载)spring配置hibernate 事务。 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring和Hibernate整合后,通過Hibernate API進(jìn)行數(shù)據(jù)庫操作時(shí)發(fā)現(xiàn)每次都要opensession,close,beginTransaction,commit,這些都是重復(fù)的工作,我們可以把事務(wù)管理部分交給spring框架完成。

?

配置事務(wù)(xml方式)

使用spring管理事務(wù)后在dao中不再需要調(diào)用beginTransaction和commit,也不需要調(diào)用session.close(),使用API ?sessionFactory.getCurrentSession()來替代sessionFactory.openSession()

1 @Repository2 public class UserDaoImpl implements UserDao {3 @Autowired4 private SessionFactory sessionFactory;5 6 public User findUserById(int id) {7 Session session = sessionFactory.getCurrentSession();8 User user = (User)session.get(User.class, id);9 session.delete(user); 10 11 return user; 12 } 13 }

采用getCurrentSession()創(chuàng)建的session會綁定到當(dāng)前線程中,而采用openSession()創(chuàng)建的session則不會。

采用getCurrentSession()創(chuàng)建的session在commit或rollback時(shí)會自動關(guān)閉,而采用openSession()創(chuàng)建的session必須手動關(guān)閉。

使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:

* 如果使用的是本地事務(wù)(jdbc事務(wù))

<property name="hibernate.current_session_context_class">thread</property>

* 如果使用的是全局事務(wù)(jta事務(wù))

<property name="hibernate.current_session_context_class">jta</property>

如果采用的時(shí)Hibernate4,使用getCurrentSession()必須配置事務(wù),否則無法取到session

?

applicationContext.xml配置

1 <?xml version="1.0" encoding="UTF-8"?>2 <beans3 xmlns="http://www.springframework.org/schema/beans"4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5 xmlns:p="http://www.springframework.org/schema/p"6 xmlns:context="http://www.springframework.org/schema/context"7 xmlns:aop="http://www.springframework.org/schema/aop"8 xmlns:tx="http://www.springframework.org/schema/tx"9 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 10 xmlns:cache="http://www.springframework.org/schema/cache" 11 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 15 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> 17 18 <context:component-scan base-package="dao"/> 19 <context:component-scan base-package="service"/> 20 <context:component-scan base-package="test"/> 21 22 <context:property-placeholder location="classpath:dbcp.properties"/> 23 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 24 <property name="driverClassName" value="${driverClassName}" /> 25 <property name="url" value="${url}" /> 26 <property name="username" value="${mysqlusername}" /> 27 <property name="password" value="${mysqlpassword}" /> 28 <property name="maxActive" value="${maxActive}" /> 29 <property name="maxIdle" value="${maxIdle}" /> 30 <property name="minIdle" value="${minIdle}" /> 31 <property name="maxWait" value="${maxWait}" /> 32 <property name="initialSize" value="${initialSize}" /> 33 <property name="logAbandoned" value="${logAbandoned}" /> 34 <property name="removeAbandoned" value="${removeAbandoned}" /> 35 <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> 36 <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> 37 <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}" /> 38 </bean> 39 40 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource" /> 42 43 <property name="hibernateProperties"> 44 <props> 45 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 46 <prop key="hibernate.show_sql">true</prop> 47 <prop key="current_session_context_class">thread</prop> 48 </props> 49 </property> 50 51 <property name="packagesToScan"> 52 <list> 53 <value>po</value> 54 </list> 55 </property> 56 </bean> 57 58 59 <!--hibernate4必須配置為開啟事務(wù) 否則 getCurrentSession()獲取不到--> 60 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 61 <property name="sessionFactory" ref="sessionFactory"></property> 62 </bean> 63 64 <tx:advice id="txAdvice" transaction-manager="txManager"> 65 <tx:attributes> 66 <tx:method name="find*" propagation="REQUIRED" /> 67 <tx:method name="*" read-only="true"/> 68 </tx:attributes> 69 </tx:advice> 70 71 <aop:config proxy-target-class="true"> 72 <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* dao.*.*(..))"/> --> 73 <aop:pointcut expression="execution(* dao.*.*(..))" id="pointcut"/> 74 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> 75 </aop:config> 76 77 </beans>

Spring中Propagation類的事務(wù)屬性詳解:

PROPAGATION_REQUIRED:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。?

PROPAGATION_SUPPORTS:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。?

PROPAGATION_MANDATORY:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。?

PROPAGATION_REQUIRES_NEW:新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。

PROPAGATION_NOT_SUPPORTED:以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。?

PROPAGATION_NEVER:以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。?

PROPAGATION_NESTED:支持當(dāng)前事務(wù),如果當(dāng)前事務(wù)存在,則執(zhí)行一個(gè)嵌套事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。

?

配置事務(wù)(聲明方式)

需要在xml配制中設(shè)置<tx:annotation-driven?transaction-manager="transactionManager">?

1 <?xml version="1.0" encoding="UTF-8"?>2 <beans3 xmlns="http://www.springframework.org/schema/beans"4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5 xmlns:p="http://www.springframework.org/schema/p"6 xmlns:context="http://www.springframework.org/schema/context"7 xmlns:aop="http://www.springframework.org/schema/aop"8 xmlns:tx="http://www.springframework.org/schema/tx"9 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 10 xmlns:cache="http://www.springframework.org/schema/cache" 11 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 15 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> 17 18 <context:component-scan base-package="dao"/> 19 <context:component-scan base-package="service"/> 20 <context:component-scan base-package="test"/> 21 22 <context:property-placeholder location="classpath:dbcp.properties"/> 23 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 24 <property name="driverClassName" value="${driverClassName}" /> 25 <property name="url" value="${url}" /> 26 <property name="username" value="${mysqlusername}" /> 27 <property name="password" value="${mysqlpassword}" /> 28 <property name="maxActive" value="${maxActive}" /> 29 <property name="maxIdle" value="${maxIdle}" /> 30 <property name="minIdle" value="${minIdle}" /> 31 <property name="maxWait" value="${maxWait}" /> 32 <property name="initialSize" value="${initialSize}" /> 33 <property name="logAbandoned" value="${logAbandoned}" /> 34 <property name="removeAbandoned" value="${removeAbandoned}" /> 35 <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> 36 <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> 37 <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}" /> 38 </bean> 39 40 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource" /> 42 43 <property name="hibernateProperties"> 44 <props> 45 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 46 <prop key="hibernate.show_sql">true</prop> 47 <prop key="current_session_context_class">thread</prop> 48 </props> 49 </property> 50 51 <property name="packagesToScan"> 52 <list> 53 <value>po</value> 54 </list> 55 </property> 56 </bean> 57 58 59 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 60 <property name="sessionFactory" ref="sessionFactory"></property> 61 </bean> 62 <tx:annotation-driven transaction-manager="txManager"/> 63 64 </beans>

?

事物注解方式: @Transactional

當(dāng)標(biāo)于類前時(shí),標(biāo)示類中所有方法都進(jìn)行事物處理,以下代碼在service層進(jìn)行事務(wù)處理(給Service層配置事務(wù)是比較好的方式,因?yàn)橐粋€(gè)Service層方法操作可以關(guān)聯(lián)到多個(gè)DAO的操作。在Service層執(zhí)行這些Dao操作,多DAO操作有失敗全部回滾,成功則全部提交。)

1 @Service2 @Transactional3 public class UserServiceImpl implements UserService {4 @Autowired5 private UserDao userDao;6 7 public User getUserById(int id) {8 return userDao.findUserById(id);9 } 10 }

?

當(dāng)類中某些方法不需要事物時(shí):

1 @Service2 @Transactional3 public class UserServiceImpl implements UserService {4 @Autowired5 private UserDao userDao;6 7 @Transactional(propagation = Propagation.NOT_SUPPORTED)8 public User getUserById(int id) {9 return userDao.findUserById(id); 10 } 11 }

?

@Transactional(propagation=Propagation.REQUIRED)?

如果有事務(wù), 那么加入事務(wù), 沒有的話新建一個(gè)(默認(rèn)情況下)
@Transactional(propagation=Propagation.NOT_SUPPORTED)?
容器不為這個(gè)方法開啟事務(wù)
@Transactional(propagation=Propagation.REQUIRES_NEW)?
不管是否存在事務(wù),都創(chuàng)建一個(gè)新的事務(wù),原來的掛起,新的執(zhí)行完畢,繼續(xù)執(zhí)行老的事務(wù)
@Transactional(propagation=Propagation.MANDATORY)?
必須在一個(gè)已有的事務(wù)中執(zhí)行,否則拋出異常
@Transactional(propagation=Propagation.NEVER)?
必須在一個(gè)沒有的事務(wù)中執(zhí)行,否則拋出異常(與Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.SUPPORTS)?
如果其他bean調(diào)用這個(gè)方法,在其他bean中聲明事務(wù),那就用事務(wù).如果其他bean沒有聲明事務(wù),那就不用事務(wù).

事物超時(shí)設(shè)置:
@Transactional(timeout=30) //默認(rèn)是30秒

事務(wù)隔離級別:
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
讀取未提交數(shù)據(jù)(會出現(xiàn)臟讀, 不可重復(fù)讀) 基本不使用
@Transactional(isolation = Isolation.READ_COMMITTED)
讀取已提交數(shù)據(jù)(會出現(xiàn)不可重復(fù)讀和幻讀)
@Transactional(isolation = Isolation.REPEATABLE_READ)
可重復(fù)讀(會出現(xiàn)幻讀)
@Transactional(isolation = Isolation.SERIALIZABLE)
串行化

MYSQL: 默認(rèn)為REPEATABLE_READ級別
SQLSERVER: 默認(rèn)為READ_COMMITTED

臟讀?: 一個(gè)事務(wù)讀取到另一事務(wù)未提交的更新數(shù)據(jù)
不可重復(fù)讀?: 在同一事務(wù)中, 多次讀取同一數(shù)據(jù)返回的結(jié)果有所不同, 換句話說,?
后續(xù)讀取可以讀到另一事務(wù)已提交的更新數(shù)據(jù). 相反, "可重復(fù)讀"在同一事務(wù)中多次
讀取數(shù)據(jù)時(shí), 能夠保證所讀數(shù)據(jù)一樣, 也就是后續(xù)讀取不能讀到另一事務(wù)已提交的更新數(shù)據(jù)
幻讀?: 一個(gè)事務(wù)讀到另一個(gè)事務(wù)已提交的insert數(shù)據(jù)

Spring整合hibernate4:事務(wù)管理,布布扣,bubuko.com

Spring整合hibernate4:事務(wù)管理

標(biāo)簽:des???style???blog???http???color???使用???

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhangkaikai/p/6856180.html

總結(jié)

以上是生活随笔為你收集整理的(转载)spring配置hibernate 事务。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。