hibernate是jdbc輕量級的封裝,本身不具備事務管理的能力,在事物管理層面,一般是委託於底層的jdbc和jta來完成調度的。以下這篇文章主要跟大家介紹了Java事務管理學習之Hibernate的相關資料,需要的朋友可以參考下。
環境與版本
hibernate 版本:Hibernate 4.2.2 (下載後的檔案名稱為hibernate-release-4.2. 2.Final.zip,解壓縮目錄hibernate-release-4.2.2.Final)
資料庫: Oracle 10g
理論說明
Session有以下的特點
commit() 方法被呼叫時
實例程式碼
<?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@XX.26.158.43:1521:orcl </property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <mapping resource="com/oscar999/trans/hibernate/Product.hbm.xml"/> </session-factory> </hibernate-configuration>
/** * @Title: Product.java * @Package com.oscar999.trans.hibernate * @Description: * @author XM * @date Feb 15, 2017 1:44:47 PM * @version V1.0 */ package com.oscar999.trans.hibernate; import java.io.Serializable; /** * @author XM * */ public class Product implements Serializable { public Product() { } private Integer id; private String name; private String price; private static final long serialVersionUID = 1L; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.oscar999.trans.hibernate.Product" table="TEST_PRODUCT"> <id name="id" column="id" type="java.lang.Integer"> <generator class="assigned" /> </id> <property name="name" column="name" type="java.lang.String" not-null="true" unique="true" length="20" /> <property name="price" column="price" type="java.lang.String" not-null="false" unique="false" length="20" /> </class> </hibernate-mapping>
/** * @Title: TestMain.java * @Package com.oscar999.trans.hibernate * @Description: * @author XM * @date Feb 15, 2017 2:02:17 PM * @version V1.0 */ package com.oscar999.trans.hibernate; import java.io.File; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; /** * @author XM * */ public class TestMain { /** * @param args */ public Session getSession() { Session session = null; Configuration conf = new Configuration().configure(new File("src/com/oscar999/trans/hibernate/hibernate.cfg.xml")); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry(); SessionFactory sf = conf.buildSessionFactory(serviceRegistry); session = sf.openSession(); return session; } public void addProduct(Session session, String name, String price) { Transaction t = session.beginTransaction(); // 1. comment1 Product product = new Product(); product.setId(1); product.setName(name); product.setPrice(price); try { session.save(product); t.commit(); // 2. comment2 } catch (Exception e) { t.rollback(); } } public static void main(String[] args) { // TODO Auto-generated method stub TestMain testMain = new TestMain(); Session session = testMain.getSession(); testMain.addProduct(session, "product1", "price1"); if (session != null && session.isOpen()) { session.close(); } } }
說明:
product.setId(1); 否則無法加入成功
Transaction t = session. beginTransaction();
#hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory #hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory
conn.setAutoCommit(false) ,不像一般的JDBC,預設都是true,所以你最後不寫commit也沒關係,由於Hibernate已經把AutoCommit給關掉了,所以用Hibernate的時候,你在程式中不寫Transaction的話,資料庫根本沒有反應。
JDBC和Hibernate 處理事務的關聯
Hibernate本身並沒有實作自己的事務管理功能,而是對底層JDBC事務或JTA事務的輕量級封裝
JTA
JTA来管理跨Session的长事务,那么就需要使用JTATransaction
javax.transaction.UserTransaction tx = new InitialContext();.lookup("javax.transaction.UserTransaction");; Session s1 = sf.openSession();; ... s1.flush();; s1.close();; ... Session s2 = sf.openSession();; ... s2.flush();; s2.close();; tx.commit();;
Hibernate Transaction是从Session中获得的,tx = session.beginTransaction()
,最后要先提交tx,然后再session.close
,这完全符合JDBC的Transaction的操作顺序,但是这个顺序是和JTA的Transactioin操作顺序彻底矛盾的!!! JTA是先启动Transaction,然后启动Session,关闭Session,最后提交Transaction,因此当你使用JTA的Transaction的时候,那么就千万不要使用Hibernate的Transaction,而是应该像我上面的JTA的代码片断那样使用才行。
总结
以上是Java事務管理學習之Hibernate詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!