Spring + Hibernate =“手动”交易如何 [英] Spring + Hibernate = "manual" transactions how-to
问题描述
我的webapp(Spring3 + Hibernate3)总是与服务类一起使用,这些服务是用 @Transactional 和这个配置注释的:
< tx:annotation-driven transaction-manager =transactionManager/>
< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =mySessionFactory/>
< / bean>
现在...我在Google AppEngine上。由于我还不知道的一些令人讨厌的原因,@Transactional不起作用。它使用javax.naming中的某个类,该类没有列入白名单。结果是:
$ b
创建名为'mySessionFactory'的bean时出错:后处理
FactoryBean的对象失败;嵌套异常是
java.lang.SecurityException:无法获得类
的成员org.hibernate.impl.SessionFactoryImpl
请不要问我为什么....:-\
使用Spring的HibernateTemplate而不是我的dao(它使用原始会话工厂)解决了问题,但我知道它已经过时了。
因此,我想尝试使用手动旧式交易。问题:
至于做什么,您可以选择:
-
依赖于另一个JPA提供程序
-
根本不使用ORM, JdbcTemplate(我的最爱)
我不确定为什么你需要使用编程事务管理,因为Hibernate是你问题的根源,但如果你只是喜欢知道如何,这里是一个草案:
public class SomeService implements SomeInterface {
private SomeDao thisDaoWrapsJdbcTemplate;
私人PlatformTransactionManager transactionManager;
public void setTransactionManager(PlatformTransactionManager transactionManager){
this.transactionManager = transactionManager;
}
public void doBusiness(Business:business){
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
尝试{
//在这里做生意
金钱= Money.LOTS_OF
...
//将钱汇入..
thisDaoWrapsJdbcTemplate.depositLotsOfMoney(money)
transactionManager.commit(status);
$ b $ catch(DataAccessException dae){
transactionManager.rollback(status);
扔dae;
}
return;
}
My webapp (Spring3 + Hibernate3) always worked with services class-annotated with @Transactional and this configuration:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
Now... I'm on Google AppEngine. For some nasty reason I don't know yet, @Transactional does not work. It uses some class in javax.naming, which is not whitelisted. It ends up with:
Error creating bean with name 'mySessionFactory': Post-processing of the FactoryBean's object failed; nested exception is java.lang.SecurityException: Unable to get members for class org.hibernate.impl.SessionFactoryImpl
Please don't ask me why.... :-\
Using Spring's HibernateTemplate instead of my dao (which uses raw session factory) solved the problem, but I know it's a little obsolete.
So, I want to try using manual old style transactions. Questions:
- where? I'd like to keep the transactions in the service layer.
- how?
SessionFactoryImpl
dependency is not in Google App Engine whitelist. There is a number of Google hits discussing it.
As far as "what to do", you have options:
Depend on on another JPA provider
Don't use ORM at all, and go native with Spring's JdbcTemplate (my favorite)
I am not sure why you need to use a programmatic transaction management since Hibernate is the root of your problem, but if you just like to know how, here is a draft:
public class SomeService implements SomeInterface {
private SomeDao thisDaoWrapsJdbcTemplate;
private PlatformTransactionManager transactionManager;
public void setTransactionManager( PlatformTransactionManager transactionManager ) {
this.transactionManager = transactionManager;
}
public void doBusiness( Business: business ) {
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction( def );
try {
// do business here
Money money = Money.LOTS_OF
...
// wire the money in..
thisDaoWrapsJdbcTemplate.depositLotsOfMoney( money )
transactionManager.commit( status );
} catch ( DataAccessException dae ) {
transactionManager.rollback( status );
throw dae;
}
return;
}
这篇关于Spring + Hibernate =“手动”交易如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!