您的位置:首页 > 其它

Service层进行事务管理,Dao层获得session进行事务处理抛异常的情况

2014-01-20 23:04 555 查看
1. spirng中配置service层的事务管理
在一个web项目中,如果使用SSH框架,一般在spring中配置事务管理。我们一般不会在dao层使用transaction,事务被配置在service层上更为合理,因为业务层方法表示逻辑上的一个原子操作。

spring中配置service层的事务管里,配置文件中的配置如下:

<bean id="txProxy"

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames">

<list>

<value>*Service</value> //这里表示

</list>

</property>

<property name="interceptorNames">

<list>

<value>transactionInterceptor</value>

</list>

</property>

</bean>

<bean id="txManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 配置事务拦截器-->

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">

<property name="transactionManager">

<ref bean="txManager" />

</property> <!-- 配置事务属性 -->

<property name="transactionAttributes">

<props>

<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>

</props>

</property>

</bean>

2,我尝试在Dao层获取sesssion,然后进行事务处理。代码如下

Session session=manureDao.getHibernateTemplate().getSessionFactory().getCurrentSession();

session.beginTransaction();

ManureInfo manureInfo=(ManureInfo)session.load(ManureInfo.class, 15);

System.out.println( manureInfo.getAreaNum());

session.getTransaction().commit();

session.close();

然后报以下异常:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)

at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)

at cn.sinobest.scscms.test.process.TestManure.testUpdateManureInfo(TestManure.java:81)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:274)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:242)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:58)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:240)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:48)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:233)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)

at org.junit.runners.ParentRunner.run(ParentRunner.java:303)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

原因:使用sessionfactory.getcurrentsession执行hibernate操作时,hibernate的操作默认必须包含在一个transaction中,

也就是开始要用session.begiontransaction得到一个transaction 实例(譬如tx), 操作结束时在这个实例上进行事务的

提交tx.commit或回滚tx.rollback. 如果这些crud操作不被包括在一个具体的transaction中,hibernate就会抛出上述异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: