Note for hibernate 2009.1.8 - 2009.1.13

本文深入探讨了Hibernate中事务管理的多个方面,包括自动版本控制的SQL更新策略、解决LazyInitializationException的方法、JTA与JDBC事务的区别及其实现方式、事件监听模式的应用等,并详细解释了Session、TransactionFactory、TransactionManagerLookup等核心组件之间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. When automatic versioning is used, hibernate generates the following sql:

update  products  set  version = ?, name = ?, description = ?, unitCost = ?, pubTime = where  id = and  version = ?

Note that using such sql, you don't need to select the version from database to compare with current version.

2. Other ways to resolve LazyInitializationException, except Open Session in View.

3. Note that for the Transaction in jta, you can register a Synchronization, through this you can be informed before or aftter transaction commit, is Hibernate use this way to flush?

Confirmed that hibernate will register a Synchronization to JTA Transaction.

 

4. Hibernate3 use event-listener pattern to implement varied operations (like save(), update()...)

 

5. There are 3 transaction release mode : after statement, after transaction (default since 3.2), on close (after session close). Note that though connection is released "after transaction", it has no impact on lazy load, it just starts a new transaction.

 

6. TransactionFactory.Context (actually a session), JDBCContext are context for Transaction, which contains Transaction instance and provide methods for Transaction to call (callback). JDBCContext.Context (actually a session) are context for JDBCContext.

JDBCContext is also used by Session to delegate method calls for transaction and connection management.

 

7. SessionImpl can be nested in tree style, and they share the same timestamp, jdbcContext, interceptor and listeners.

 

8. Who calls TransactionFactory.Context and JDBCContext's before/after method? Both CacheSynchronization and Transaction will do.

 

9. Can I call the save() method without starting a new transaction? Yes, in case of JTA or CMT, the transaction is already started, for JDBC, it also starts a new transaction for JDBC dose not need to call a begin() method.

 

10. Why jta transaction and jdbc transaction's code style should be different? Hibernate provides JTA/CMT Transaction in order to support change to use JTA/CMT from JDBC without code change, so JTA and CMT can use JDBC transaction style, also, they can use their own style.

 

11. Explain JDBCContext's registerCallbackIfNecessary() and registerSynchronizationIfPossible() method. A callback is needed in order to flush before transaction commit, this callback can either be CacheSynchronization that register itself to jta transaction, or hibernate's JDBC/JTA/CMT Transaction. The registerSynchronizationIfPossible() method use CacheSynchronization, while registerCallbackIfNecessary() use hibernate's transaction object. The registerCallbackIfNecessary() method return true if callback is needed, if registerSynchronizationIfPossible() or registerCallbackIfNecessary() was called before, it will return false.

 

12. Those TransactionManagerLookup implementations are used by JTATransactionManager and also SessionFactoryImpl (whose getTransactionManager() method is used by JDBCContext's registerSynchronizationIfPossible()).

 

13. Can omit flush and close session before JTA transaction commit? I believe not, when transaction commit, session will flush and at that time session is closed, but still need to test.
Also, dose jta transaction need to begin before opening a new session? I believe not too.

 

14. Can omit hibernate.transaction.factory_class setting? If incorrect factory class is specified, Interceptor will get incorrect type of Transaction and fail to retrieve correct state from that transaction. If hibernate transaction objects are used by client code, incorrect factory class will cause error.

 

15. How hiberate 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 call hibernate's transaction objects.

 

16. Relationship between TransactionFactory, Transaction, TransactionManagerLookup, CacheSynchronization, JDBCContext, Interceptor.





  

 

- -! Gavin King 's comment at TransactionManagerLookup.getTransactionIdentifier: "This method was added specifically for use in WebSphere and other unfriendly JEE containers (although WebSphere is still the only known such brain-dead, sales-driven impl)."

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值