添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
追风的毛衣  ·  动态TabControl中的C# WPF ...·  5 月前    · 
气宇轩昂的自行车  ·  Mybatis 动态 SQL ...·  1 年前    · 
苦恼的伤疤  ·  Python ...·  1 年前    · 


http://www.javaeye.com/topic/11063?page=1


---mixed ORM and JDBC usage is a feature of Spring DAO



这是Rod Johnson在Spring官方上说的....以前我的理解是可以在同一个事务里


混合JDBC和ORM 编程(不考虑JTA).但我发现我错了..



比如有这样一个业务方法



void doBusiness(){ 
  doAction1(); 使用JdbcTemplete 
  doAction2(); 使用JdoDaoSupport或HBDaoSupport 
}



但这样缺是行不通的,Spring只是能拿到HB和JDBC的同一个数据源(DataSource),却拿不到Hibernate和Jdbc的同一个底层Connection的连接


导致这2个方法将分别完成,并不能处在同一个事务,而且我运行的后还出现连接未释放的情况.(他们单独拿出来当然是正确的)...



难道rod的话的意思只是可以在不同的方法里,使jdbc和ORM拿到同一个数据源,mixed JDBC and ORM ?????郁闷中...



回帖:


楼主以及二楼的朋友的论断错误倒也罢了,那种肯定的,结论性总结的态度很容易误导初学者的学习和理解。



提个小小的建议:下结论前要进行充分的考证,我们技术工作者尤其需要严谨的态度。需要用证据来说话。



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代码

1. HibernateTransactionObject txObject = new HibernateTransactionObject();;  
2. txObject.setSavepointAllowed(isNestedTransactionAllowed(););;  
4. if (TransactionSynchronizationManager.hasResource(getSessionFactory(););); {  
5.     SessionHolder sessionHolder =  
6.             (SessionHolder); TransactionSynchronizationManager.getResource(getSessionFactory(););;  
7.     if (logger.isDebugEnabled();); {  
8.         logger.debug("Found thread-bound session [" + sessionHolder.getSession(); +  
9.                 "] for Hibernate transaction");;  
10.     }  
11.     txObject.setSessionHolder(sessionHolder, false);;  
12.     if (getDataSource(); != null); {  
13.         ConnectionHolder conHolder = (ConnectionHolder);  
14.                 TransactionSynchronizationManager.getResource(getDataSource(););;  
15.         txObject.setConnectionHolder(conHolder);;  
16.     }  
17. }  
19. 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代码

1. Connection con = session.connection();;  
2. Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);;  
3. txObject.setPreviousIsolationLevel(previousIsolationLevel);;  
5. ...........  
7. if (getDataSource(); != null); {  
8.     ConnectionHolder conHolder = new ConnectionHolder(con);;  
9.     if (definition.getTimeout(); != TransactionDefinition.TIMEOUT_DEFAULT); {  
10.         conHolder.setTimeoutInSeconds(definition.getTimeout(););;  
11.     }  
12.     if (logger.isDebugEnabled();); {  
13.         logger.debug("Exposing Hibernate transaction as JDBC transaction [" +  
14.                 conHolder.getConnection(); + "]");;  
15.     }  
16.     TransactionSynchronizationManager.bindResource(getDataSource();, conHolder);;  
17.     txObject.setConnectionHolder(conHolder);;  
18. }  
19. // bind the session holder to the thread  
20. if (txObject.isNewSessionHolder();); {  
21.     TransactionSynchronizationManager.bindResource(getSessionFactory();, txObject.getSessionHolder(););;  
22. }  
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(););;     }


由此可以看出,在真正启动一个事务时,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.

axios基础应用实战 axios入门

vue项目中axios的基本使用和简单封装axios中文文档官网  http://www.axios-js.com/docs/一:不封装直接使用  npm install axios  在main.js里面引用如下1 import axios from 'axios' 2 Vue.prototype.$axios = axios;   在自己的组件中然后使用 getInfo()

boot redis链接 spring springboot怎么连接redis

背景这篇文章不会介绍如何下载Redis,默认从下载安装完Redis后开始讲,主要描述spring boot中怎么整合Redis并使用Redis,步骤超级简单,只要按照如下步骤做,肯定能成功。第一步 导入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId

2.SpringCloud02-Nacos配置管理(热更新、共享、集群)、Feign远程调用(优美封装Http,抽取feign-api)、Gateway服务网关(路由规则、断言工厂、过滤器、跨域问题) 3.在线JS代码混淆加密工具真的能实现JS代码加密吗? 4.文件完整性hash验证demo(python脚本) 5.如何处理客户投诉问题?7个技巧妥善处理