在對數據庫進行操作時,有的時候會出現級聯操作的情況。爲了保證數據的一致性,我們會考慮使用事務。事務分爲編程式事務和聲明式事務兩種。先看一下編程式事務:
編程式事務分爲兩種實現形式。區別並不是很大。openSession和getCurrentSession兩種形式。
openSession形式
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
session.save(user);
Log log = new Log();
log.setType("操作日誌");
LogManager logManager = new LogManagerImpl();
logManager.addLog(log);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
getCurrentSession方式
Session session = null;
try {
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
Log log = new Log();
log.setType("操作日誌");
LogManager logManager = new LogManagerImpl();
logManager.addLog(log);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
配置文件中配置
<property name="hibernate.current_session_context_class">thread</property>
通過以上兩種方式的實現,我們可以看出
1、 openSession必須關閉,currentSession在事務結束後自動關閉
openSession沒有和當前線程綁定,currentSession和當前線程綁定
2、如果使用currentSession需要在hibernate.cfg.xml文件中進行配置:
* 如果是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
* 如果是全局事務(jta事務)
<property name="hibernate.current_session_context_class">jta</property>
有了spring之後,他對事務進行了很好的封裝。有了面向切面的AOP,實現事務變得越來越方便。我們不用再每個需要事務的方法中去開啓關閉事務,把這些直接交給配置文件,代碼中看不到事務的影子,悄無聲息的就把事務給實現了。
聲明式事務具體實現:
applicationContext配置文件:
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 那些類那些方法使用事務 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.manager.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
<!-- 事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
這是關於事務的配置信息,除了這些,applicationContext關於bean的配置也有所變化。
<bean id="userManager" class="com.bjpowernode.usermgr.manager.UserManagerImpl">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="logManager" ref="logManager"/>
</bean>
<bean id="logManager" class="com.bjpowernode.usermgr.manager.LogManagerImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
由於實現聲明式事務,需要繼承HibernateDaoSupport,所以,配置文件中多了一個屬性。
再看一下具體應用事務的代碼的實現:
public class UserManagerImpl extends HibernateDaoSupport implements UserManager {
private LogManager logManager;
public void addUser(User user)
throws Exception {
this.getHibernateTemplate().save(user);
Log log = new Log();
log.setType("操作日誌");
logManager.addLog(log);
thrownew Exception();
}
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
}
}
可以看到代碼中省去了開啓,關閉session等步驟。只要一句this.getHibernateTemplate().save(user);就實現了。
總結一下聲明式事務的實現:
1、聲明式事務配置
* 配置SessionFactory
* 配置事務管理器
* 事務的傳播特性
* 那些類那些方法使用事務
2、編寫業務邏輯方法
* 繼承HibernateDaoSupport類,使用HibernateTemplate來持久化,HibernateTemplate是Hibernate Session的輕量級封裝
* 默認情況下運行期異常纔會回滾(包括繼承了RuntimeException子類),普通異常是不回滾的
* 編寫業務邏輯方法時,最好將異常一直向上拋出,在表示層(struts)處理
* 關於事務邊界的設置,通常設置到業務層,不要添加到Dao上
說到事務,那如果程序出錯事務就要回滾。但並不是所有的異常都會回滾的。只有運行期異常纔會回滾,其他的一般錯誤不會回滾。如Throws exception就屬於一般異常,默認不回滾。但是,spring實現了靈活性,什麼樣的事務可以回滾是可以配置的。
要使用事務,需要了解事務的傳播特性和隔離級別。簡單的介紹一下:
瞭解事務的幾種傳播特性
1. PROPAGATION_REQUIRED: 如果存在一個事務,則支持當前事務。如果沒有事務則開啓
2. PROPAGATION_SUPPORTS: 如果存在一個事務,支持當前事務。如果沒有事務,則非事務的執行
3. PROPAGATION_MANDATORY: 如果已經存在一個事務,支持當前事務。如果沒有一個活動的事務,則拋出異常。
4. PROPAGATION_REQUIRES_NEW: 總是開啓一個新的事務。如果一個事務已經存在,則將這個存在的事務掛起。
5. PROPAGATION_NOT_SUPPORTED: 總是非事務地執行,並掛起任何存在的事務。
6. PROPAGATION_NEVER: 總是非事務地執行,如果存在一個活動事務,則拋出異常
7. PROPAGATION_NESTED:如果一個活動的事務存在,則運行在一個嵌套的事務中. 如果沒有活動事務,則按TransactionDefinition.PROPAGATION_REQUIRED 屬性執行
Spring事務的隔離級別
1. ISOLATION_DEFAULT: 這是一個PlatfromTransactionManager默認的隔離級別,使用數據庫默認的事務隔離級別.
另外四個與JDBC的隔離級別相對應
2. ISOLATION_READ_UNCOMMITTED: 這是事務最低的隔離級別,它充許令外一個事務可以看到這個事務未提交的數據。
這種隔離級別會產生髒讀,不可重複讀和幻像讀。
3. ISOLATION_READ_COMMITTED: 保證一個事務修改的數據提交後才能被另外一個事務讀取。另外一個事務不能讀取該事務未提交的數據
4. ISOLATION_REPEATABLE_READ: 這種事務隔離級別可以防止髒讀,不可重複讀。但是可能出現幻像讀。
它除了保證一個事務不能讀取另一個事務未提交的數據外,還保證了避免下面的情況產生(不可重複讀)。
5. ISOLATION_SERIALIZABLE 這是花費最高代價但是最可靠的事務隔離級別。事務被處理爲順序執行。
除了防止髒讀,不可重複讀外,還避免了幻像讀。