`
semi_sleep
  • 浏览: 99416 次
  • 性别: 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?

分享到:
评论

相关推荐

    Could not roll back Hibernate transaction.doc

    Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: JDBC rollback failed 这表明Hibernate事务回滚操作失败,导致事务不能正确回滚。 二、问题原因 该问题的...

    Hibernate+Transaction+myeclipse6+事例+工程源码.rar

    介绍Hibernate+Transaction+myeclipse6+事例的应用参考工程源码.

    spring-hibernate-maven-transaction整合

    spring-hibernate-maven-transaction整合

    Hibernate_Session_Transaction

    可以对hibernate的 Session 和 Transaction 有更深入的认识 希望对一些概念比较模糊的人 有帮助

    Beginning Hibernate: For Hibernate 5

    Get started with the Hibernate 5 persistence layer and gain a clear introduction to the current standard for object-relational persistence in Java. This updated edition includes the new Hibernate 5.0 ...

    软件依赖包(Spring AOP+Hibernate Transaction)

    该软件依赖包,为实例spring aop实现Hinernate事务操作软件包

    Beginning.Hibernate.For.Hibernate.5.4th.Edition

    Get started with the Hibernate 5 persistence layer and gain a clear introduction to the current standard for object-relational persistence in Java. This updated edition includes the new Hibernate 5.0 ...

    Oracle Services for Microsoft Transaction Server Developer's

    Oracle Services for Microsoft Transaction Server Developer's Guide

    HibernateSession , Transaction 研究

    NULL 博文链接:https://stewchicken.iteye.com/blog/938875

    PureDataSystem for Transaction探析

    PureDataSystem for Transaction探析

    Hibernate学习笔记和资料

    hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...

    hibernate.properties

    #hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup #hibernate.transaction.manager_lookup_class org.hibernate.transaction....

    基于dubbo和hibernate的例子

    基于dubbo和hibernate的分层例子,并有一个dubbo client模拟消费者

    Hibernate Web应用的开发步骤

    Hibernate Web应用的开发一般经过以下几个步骤: (1)创建数据库。 (2)将Hibernate所需的JAR包复制到WEB-...(8)通过创建的Transaction实例进行事务管理。 (9)通过创建的Query或Criteria实例实现数据库的查询。

    Hibernate所需jar包

    antlr-2.7.7 c3p0-0.9.2.1 dom4j-1.6.1 hibernate-c3p0-4.3.5.Final hibernate-commons-annotations-...jboss-transaction-api_1.2_spec-1.0.0.Final mchange-commons-java-0.2.3.4 mysql-connector-java-5.1.30-bin

    Beginning Hibernate, 3rd Edition

    This book packs in information about the release of the Hibernate 4.x persistence layer and provides a clear introduction to the current standard for object-relational persistence in Java. And since ...

    Hibernate稳定版(hibernate-release-5.2.18.Final.zip)

    Hibernate稳定版(hibernate-release-5.2.18.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。

    hibernate4.3完整jar包

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java...Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。

    Hibernate Recipes A Problem-Solution Approach

    How to use Hibernate for heavy-duty enterprise transaction–based systems Who is this book for? This book is for experienced Java developers looking to use Hibernate, but is also appropriate for Java ...

    Hibernate体系结构的概要图

    Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。这6个核心接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务...

Global site tag (gtag.js) - Google Analytics