ale*_*543 9 java spring hibernate transactions jdbc
我有一个方法,标记为@Transactional.它由几个函数组成,其中一个使用JDBC,第二个是Hibernate,第三个是JDBC.问题是Hibernate函数所做的更改在最后一个与JDBC一起使用的函数中是不可见的.
@Transactional
void update() {
jdbcUpdate1();
hibernateupdate1();
jdbcUpdate2(); // results of hibernateupdate1() are not visible here
}
Run Code Online (Sandbox Code Playgroud)
所有函数都配置为使用相同的数据源:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="targetDataSource"/>
</bean>
<bean id="targetDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="true" scope="singleton">
<!-- settings here -->
</bean>
Run Code Online (Sandbox Code Playgroud)
myDataSource bean用于代码中.myDataSource.getConnection()用于处理jdbc函数和中的连接
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
...
}
});
Run Code Online (Sandbox Code Playgroud)
用于休眠函数.谢谢.
Boz*_*zho 11
首先,避免在使用hibernate时使用JDBC.
然后,如果你真的需要它,请使用Session.doWork(..).如果你的Hibernate的版本还没有这种方法,获得了Connection从session.connection().
| 归档时间: |
|
| 查看次数: |
6265 次 |
| 最近记录: |