日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring 同时配置hibernate and jdbc 事务

發布時間:2024/4/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 同时配置hibernate and jdbc 事务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/mypop/article/details/6120377

在項目中同時用到了JDBC和Hibernate,分別配置了各自的事務,同時配置了不同的tx:annotation-driven。

但是在實際使用中,只有配置在前的tx:annotation-driven起了作用,另外一個tx:annotation-driven不起作用。

?

?============

給一個TransactionProxyFactoryBean(使用DataSourceTransactionManager)配置的service加上@Transactional

(用HibernateTransactionManager聲明的),則會使用DataSourceTransactionManager事務

?

===============

http://zhangxiaoming.iteye.com/blog/307670

spring 中 Hibernate 事務和JDBC事務嵌套問題

JDBCHibernateSpring配置管理ORM
最近開發中,遇到了這樣一個問題。

1.系統中,事務是有spring管理的。

2.系統中,即用到了JDBC,又用到了Hibernate。

3.spring管理了jdbc事務,也管理了Hibernate事務。


如上3點所述,存在這樣的情況:

配置了jdbc事務的service,注入了配置了hibernate事務的service。這時,執行的時候,系統就會拋出異常:
org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC connection found - HibernateTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single HibernateTransactionManager for all transactions on a single DataSource, no matter whether Hibernate or JDBC access.

但是,由配置了hibernate事務的service,注入了配置了jdbc事務的service。就能正常運行。

不知道大家有沒有遇到過類似的問題。一起討論一下。

================

http://www.iteye.com/topic/37707

最近摸索了一下Hibernate與JDBC(iBATIS)事務整合問題,算是有點收獲,寫出來和大家討論一下吧。。。

一般大家都會使用Spring聲明型事務 transactionAttributes 為 PROPAGATION_REQUIRED

Hibernate 使用 HibernateTransactionManager 、JDBC(iBATIS) 使用 DataSourceTransactionManager

當需要將它們整合到一個事務中的時候

普通的做法是配置統一的DataSource,?Hibernate與JDBC(iBATIS) 都使用HibernateTransactionManager

壇子里有篇帖子已經詳細論述了這種整合方案 http://www.iteye.com/topic/11063?page=2

------------------------------------------------------

Hibernate與JDBC(iBATIS) 都使用DataSourceTransactionManager又可不可以呢?

普遍的看法是 NO! 認為DataSourceTransactionManager根本就不處理Hibernate的session,事務當然無法實現。。。

但事實是否真的如此呢?

答案同樣是 NO!?http://forum.springframework.org/archive/index.php/t-9878.html

Juergen Hoeller:
Just if you proxy your JDBC DataSource with a TransactionAwareDataSourceProxy (available since Spring 1.1 RC1) and pass that proxy to your LocalSessionFactoryBean, you could use DataSourceTransactionManager in combination with Hibernate.

也就是說配置 sessionFactory ?的 useTransactionAwareDataSource 為 true

Hibernate與JDBC(iBATIS)? 都使用 DataSourceTransactionManager 同樣可以保證事務

原理就是保證了 connection 的唯一性。

====================http://www.iteye.com/topic/11063?page=2

樓主以及二樓的朋友的論斷錯誤倒也罷了,那種肯定的,結論性總結的態度很容易誤導初學者的學習和理解。

提個小小的建議:下結論前要進行充分的考證,我們技術工作者尤其需要嚴謹的態度。需要用證據來說話。

jdo dao和jdbc dao能否在同一個事務里這我不太清楚。因為我沒用過jdo daosupport。
但是jdbc daosupport和hibernate daosupport卻能被wrap到同一個事務里。成立需要幾點條件:
1、使用同一個datasource
2、事務交由hibernateTransactionManager管理
3、相關dao以及service需要使用runtime exception體系,使用spring提供的exception可以,自己封裝設計的runtime exception體系也行。

與此相關的事務代碼片斷在HibernateTransactionManager類中。最好可以把DatasourceTransactionManager和HibernateTransactionManager對比來看。
在此貼上幾個源碼片斷,多余的我就不解釋了。相信大家一看自明。
HibernateTransactionManager#doGetTransaction

Java代碼 ?
  • HibernateTransactionObject?txObject?=?new?HibernateTransactionObject();; ??
  • txObject.setSavepointAllowed(isNestedTransactionAllowed(););; ??
  • ??
  • if?(TransactionSynchronizationManager.hasResource(getSessionFactory();););?{ ??
  • ????SessionHolder?sessionHolder?= ??
  • ????????????(SessionHolder);?TransactionSynchronizationManager.getResource(getSessionFactory(););; ??
  • ????if?(logger.isDebugEnabled(););?{ ??
  • ????????logger.debug("Found?thread-bound?session?["?+?sessionHolder.getSession();?+ ??
  • ????????????????"]?for?Hibernate?transaction");; ??
  • ????} ??
  • ????txObject.setSessionHolder(sessionHolder,?false);; ??
  • ????if?(getDataSource();?!=?null);?{ ??
  • ????????ConnectionHolder?conHolder?=?(ConnectionHolder); ??
  • ????????????????TransactionSynchronizationManager.getResource(getDataSource(););; ??
  • ????????txObject.setConnectionHolder(conHolder);; ??
  • ????} ??
  • } ??
  • ??
  • return?txObject;??
  • HibernateTransactionObject txObject = new HibernateTransactionObject();;txObject.setSavepointAllowed(isNestedTransactionAllowed(););;if (TransactionSynchronizationManager.hasResource(getSessionFactory(););); {SessionHolder sessionHolder =(SessionHolder); TransactionSynchronizationManager.getResource(getSessionFactory(););;if (logger.isDebugEnabled();); {logger.debug("Found thread-bound session [" + sessionHolder.getSession(); +"] for Hibernate transaction");;}txObject.setSessionHolder(sessionHolder, false);;if (getDataSource(); != null); {ConnectionHolder conHolder = (ConnectionHolder);TransactionSynchronizationManager.getResource(getDataSource(););;txObject.setConnectionHolder(conHolder);;}}return txObject;


    由此可以看出hibernateTransactionManager可以檢測到綁定在當前線程上的connection

    HibernateTransactionManager#doBegin

    Java代碼 ?
  • Connection?con?=?session.connection();; ??
  • Integer?previousIsolationLevel?=?DataSourceUtils.prepareConnectionForTransaction(con,?definition);; ??
  • txObject.setPreviousIsolationLevel(previousIsolationLevel);; ??
  • ??
  • ........... ??
  • ??
  • if?(getDataSource();?!=?null);?{ ??
  • ????ConnectionHolder?conHolder?=?new?ConnectionHolder(con);; ??
  • ????if?(definition.getTimeout();?!=?TransactionDefinition.TIMEOUT_DEFAULT);?{ ??
  • ????????conHolder.setTimeoutInSeconds(definition.getTimeout(););; ??
  • ????} ??
  • ????if?(logger.isDebugEnabled(););?{ ??
  • ????????logger.debug("Exposing?Hibernate?transaction?as?JDBC?transaction?["?+ ??
  • ????????????????conHolder.getConnection();?+?"]");; ??
  • ????} ??
  • ????TransactionSynchronizationManager.bindResource(getDataSource();,?conHolder);; ??
  • ????txObject.setConnectionHolder(conHolder);; ??
  • } ??
  • //?bind?the?session?holder?to?the?thread ??
  • if?(txObject.isNewSessionHolder(););?{ ??
  • ????TransactionSynchronizationManager.bindResource(getSessionFactory();,?txObject.getSessionHolder(););; ??
  • }??
  • Connection con = session.connection();;Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);;txObject.setPreviousIsolationLevel(previousIsolationLevel);;..............if (getDataSource(); != null); {ConnectionHolder conHolder = new ConnectionHolder(con);;if (definition.getTimeout(); != TransactionDefinition.TIMEOUT_DEFAULT); {conHolder.setTimeoutInSeconds(definition.getTimeout(););;}if (logger.isDebugEnabled();); {logger.debug("Exposing Hibernate transaction as JDBC transaction [" +conHolder.getConnection(); + "]");;}TransactionSynchronizationManager.bindResource(getDataSource();, conHolder);;txObject.setConnectionHolder(conHolder);;}// bind the session holder to the threadif (txObject.isNewSessionHolder();); {TransactionSynchronizationManager.bindResource(getSessionFactory();, txObject.getSessionHolder(););;}


    由此可以看出,在真正啟動一個事務時,hbTxnManager會先把connection綁定到當前線程,再綁定session到當前線程,由TransactionSynchronizationManager統一管理。并且上面提到的connection是從session中取得的,也就是說,無論是jdbc dao還是hibernate dao本質上使用的是同一個database connection

    因此得出結論:HibernateTransactionManager實際上是可以同時管理由JdbcTemplate或JdbcDaoSupport實現的dao以及HibernateTemplate或HibernateDaoSupport實現的事務的。


    Rod Johnson的話:

    引用 It is possible--and sometimes useful--to have coordinated transactions for both. Your JDBC transactions will be managed by the HibernateTransactionManager if you work with the same JDBC DataSource in the same transaction. That is, create the SessionFactory using Spring's SessionFactoryBean using the same DataSource that your JdbcTemplates use.

    The only issue to watch, of course, is that you may be invalidating your Hibernate cache by JDBC changes. Generally I find it best to use JDBC to update only tables that don't have Hibernate mappings.



    Juergen Hoeller的話:

    引用 As Rod said, simply keep using HibernateTransactionManager, which auto-detects the DataSource used by Hibernate and seamlessly exposes Hibernate transactions as JDBC transactions for that DataSource. JDBC code that accesses the same DataSource via Spring will automatically participate in such transactions.

    Note that you must specify the DataSource for Hibernate via LocalSessionFactoryBean's "dataSource" property to allow HibernateTransactionManager to auto-detect it. Alternatively, you can explicitly pass the DataSource to HibernateTransactionManager's "dataSource" property.

    ?

    ?

    總結

    以上是生活随笔為你收集整理的spring 同时配置hibernate and jdbc 事务的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。