Hibernate和JDBC在一个事务中 [英] Hibernate and JDBC in one transaction
问题描述
我有一个方法,标记为@Transactional。
它由几个函数组成,其中一个使用JDBC,第二个使用JDBC - Hibernate,第三个使用JDBC。
问题在于,由Hibernate函数所做的更改在最后一些函数中不可见,这些函数可以与JDBC一起使用。
@Transactional
void update(){
jdbcUpdate1();
hibernateupdate1();
jdbcUpdate2(); //结果hibernateupdate1()在这里不可见
}
所有函数都被配置为使用相同的数据源:
< bean id =myDataSourceclass =org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy >
< property name =targetDataSourceref =targetDataSource/>
< / bean>
< bean id =targetDataSourceclass =org.apache.commons.dbcp.BasicDataSource
destroy-method =closelazy-init =truescope =单>
<! - 此处的设置 - >
< / bean>
myDataSource bean用于代码中。 myDataSource.getConnection()用于处理jdbc函数中的连接,并且使用
getHibernateTemplate()。execute(new HibernateCallback(){
public Object doInHibernate(Session session)抛出HibernateException,SQLException {
...
}
});
用于休眠功能。
Thanks。
首先,避免在使用hibernate时使用JDBC。 $ b
然后,如果您确实需要它,请使用 Session.doWork(..)
。如果您的休眠版本还没有此方法,请从 session.connection()
中获取 Connection
。
I have a method, marked as @Transactional. It consists of several functions, one of them uses JDBC and the second one - Hibernate, third - JDBC. The problem is that changes, made by Hibernate function are not visible in the last functions, that works with JDBC.
@Transactional
void update() {
jdbcUpdate1();
hibernateupdate1();
jdbcUpdate2(); // results of hibernateupdate1() are not visible here
}
All functions are configured to use the same datasource:
<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>
myDataSource bean is used in the code. myDataSource.getConnection() is used to work with connections in jdbc functions and
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
...
}
});
is used in hibernate function. Thanks.
First, avoid using JDBC when using hibernate.
Then, if you really need it, use to Session.doWork(..)
. If your hibernate version does not yet have this method, obtain the Connection
from session.connection()
.
这篇关于Hibernate和JDBC在一个事务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!