`
semi_sleep
  • 浏览: 101117 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Summary for Hibernate Transaction

阅读更多

1.Three code styles

Use hibernate's transaction:

session.beginTransaction();
// ... interact with database
session.getTransaction().commit();

Use JTA:

javax.transaction.UserTransaction tx = new InitialContext().lookup("javax.transaction.UserTransaction");    
// ... use session to interact with database
tx.commit(); 

Use CMT:

// interact with database, don't need to write any code to manage transaction

2.TransactionManagerLookup

TransactionManagerLookup is used by JDBCContext to find JTA TransactionManager to register a CacheSynchronization. Also it's used by TransactionFactory to find JNDI name for UserTransaction.

Use "hibernate.transaction.manager_lookup_class" to config this class.

TransactionManagerLookup
                       |--- -> [TransactionManager ] -> JDBCContext
                       |
                       |--- -> [UserTransaction JNDI name] -> TransactionFactory

3.TransactionFactory

TransactionFactory is used to create hibernate transaction objects. There are 3 implementations, JDBC, JTA and CMT, though hibernate transaction seems only useful for JDBC, the JTA and CMT implementations allow client codes to shift from JDBC to JTA or CMT without change.

Use "hibernate.transaction.factory_class" to config this class, note that though it may seem ok when you use JDBCTransactionFactory in JTA or CMT environment, hibernate will do his best to dectect the correct transaction mode, it may still has some problem when you use Interceptor, the Transaction object passed to the Interceptor will be JDBCTransaction in JTA or CMT enviroment. 

4.How hibernate detect the type of transaction to use automatically?

Hibernate will first try to find out whether a callback is registered and use TransactionManager to see if there's currently a transaction, if yes, it will register a synchronization, if no, it will rely on client code to use hibernate's transaction objects to manage transaction.

5.How hibernate implement auto flush and close after transaction?

If hibernate transaction is used, this is done by transaction object's commit method, if JTA or CMT is used, hibernate will register a CacheSynchronization to current transaction so that it can be notified before / after global transaction is commited.

6.Operations in sequence

Transaction calls and other operations may appear in any sequence, the order of which has sinificant impact on underlying process. Here's all the available operations: transaction begin / commit / rollback (transaction may be JDBCTransaction, JTATransaction, CMTTransaction, JTA UserTransaction or CMT declarative transaction, also, commit may trigger auto flush), open session, close session, explicit operations (like save, update, flush), lazy load.

6.1.Open session

For JTA and CMT, the openSession() method call can either be placed before or after transaction begin.

6.2.Close session

For JTA and CMT, the close() method call can either be placed before or after transaction commit. Note that when placed before transaction commit, though connection is closed, the related transaction branch will still in effect, so sqls emitted by this session can still be committed, but since the session is closed, when global transaction commits, the managedFlush() method (call by

CacheSynchronization) will not flush, this means that auto flush will be disable if session is closed before JTA or CMT transaction, so in this case, you must call flush() manually before closing the session.

6.3.Explicit operation and lazy load

These operations should appear within transaction boundary, but if they don't, for JDBC transaction, it's ok since session will always opens a new connection and set auto commit to be false before emitting any sql, this implies that a new transaction is started, for JTA or CMT transaction, the result will depend on drivers implementation, seems some will create a local transaction while others may throw an exception.

7.About first-level cache

Hibernate's first level cache is invalidated only when session is closed, this means that first-level cache can be used through several transaction. Also note that first-level cache may be used for dirty checking while flush (or auto flush during commit), this means that objects return by previous transaction can be check for dirty and has it's status sync with database in current transaction.

8.Questions?

What happen if user provided connection is used?

What if plain jdbc is expected to be used, but it's also inside JTA container?

Any impact for different connection release mode?

分享到:
评论

相关推荐

    Hibernate 5.3.1api 文档

    3. **事务管理**:在Hibernate中,Transaction接口用于管理数据库事务。事务的开始、提交和回滚操作通过Session的beginTransaction()、commit()和rollback()方法实现。 4. **查询语言**:Hibernate支持HQL...

    Hibernate+中文文档

    11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管...

    hibernate3.2中文文档(chm格式)

    11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管...

    HibernateAPI中文版.chm

    11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管...

    Hibernate Reference Documentation3.1

    1.5. Summary 2. Architecture 2.1. Overview 2.2. Instance states 2.3. JMX Integration 2.4. JCA Support 2.5. Contextual Sessions 3. Configuration 3.1. Programmatic configuration 3.2. Obtaining a ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管...

    hibernate 3.3.1.GA API

    通过深入学习这些文档,开发者可以熟练掌握Hibernate的核心概念,如Session、Transaction、Criteria查询、HQL(Hibernate Query Language)、Entity、Collection Mapping等,从而在实际开发中更加高效地操作数据库。...

    前端-后端java的Util类的工具类

    │ ToolFor9Ge.java │ ├─css │ bootstrap.css │ bootstrap.min.css │ component.css │ cylater.css │ global.css │ login.css │ reset.css │ ├─js │ │ avalon.js │ │ components.js │ │ ...

    hql文档详解 相信对初步的朋友有帮助

    for (UserSummary summary : summaries) { System.out.println(summary.getName() + ", " + summary.getAge()); } ``` #### 四、HQL高级查询 除了上述的基本查询外,HQL还支持更复杂的查询功能,如子查询、...

    spring-boot-reference.pdf

    10.1. Installation Instructions for the Java Developer 10.1.1. Maven Installation 10.1.2. Gradle Installation 10.2. Installing the Spring Boot CLI 10.2.1. Manual Installation 10.2.2. Installation with...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    In summary, "Finding Me Detecting" online certification and detecting system filled up blanks of mobile market in certification and detecting field, provoding double side access pattern, that is to ...

Global site tag (gtag.js) - Google Analytics