问题描述
Exceptioninthread"main"java.lang.IllegalStateException:ActiveSpringtransactionsynchronizationoractiveJTAtransactionwithspecified[javax.transaction.TransactionManager]requiredatorg.springframework.jdbc.support.lob.LobCreatorUtils.registerTransactionSynchronization(LobCreatorUtils.java:79)atorg.springframework.orm.hibernate3.support.AbstractLobType.nullSafeSet(AbstractLobType.java:185)atorg.hibernate.type.CustomType.nullSafeSet(CustomType.java:146)atorg.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:336)atorg.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2166)atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2412)atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2850)atorg.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)atorg.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)atorg.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)atorg.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)atorg.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)atorg.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)atorg.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407)atorg.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)atorg.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)atxiangya.dao.impl.PatientInfoDaoImpl.save(PatientInfoDaoImpl.java:36)atxiangya.dao.impl.test.Test.main(Test.java:143)<?xmlversion="1.0"encoding="UTF-8"?>applicationContext.xml文件<!--指定Spring配置文件的Schema信息--><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!--<beanid="lobHandler"lazy-init="true"class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>--><beanid="lobHandler"class="org.springframework.jdbc.support.lob.DefaultLobHandler"lazy-init="true"/><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="lobHandler"ref="lobHandler"/><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property></bean><!--tansaction--><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><tx:adviceid="txAdvice"transaction-manager="transactionManager"><!--用于配置详细的事务语义--><tx:attributes><tx:methodname="save*"propagation="REQUIRED"/><!--其他方法使用默认的事务设置--><tx:methodname="*"/></tx:attributes></tx:advice><!--AOP配置的元素--><aop:configproxy-target-class="true"><!--配置一个切入点,匹配xiangya.dao.impl包下所有以Impl结尾的类里、所有方法的执行--><aop:pointcutid="myPointcut"expression="execution(*xiangya.dao.impl.*Impl.*(..))"/><!--指定在txAdvice切入点应用txAdvice事务增强处理--><aop:advisoradvice-ref="txAdvice"pointcut-ref="myPointcut"/></aop:config><!--DAO--><beanid="daoTemplate"abstract="true"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><beanid="patientInfoDao"class="xiangya.dao.impl.PatientInfoDaoImpl"parent="daoTemplate"></bean><!--Service--><beanid="patientInfoService"class="xiangya.service.impl.PatientInfoServiceImpl"><propertyname="patientInfoDao"ref="patientInfoDao"></property></bean></beans>dao文件packagexiangya.dao;importjava.util.List;importxiangya.domain.PatientInfo;publicinterfacePatientInfoDao{voidsave(PatientInfopatientInfo);voidupdate(PatientInfopatientInfo);voiddelete(PatientInfopatientInfo);List<PatientInfo>findByPatientId(StringpatientId);List<PatientInfo>findAllPatient();}daoImpl文件packagexiangya.dao.impl;importjava.util.List;importorg.springframework.jdbc.support.lob.LobHandler;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importxiangya.dao.PatientInfoDao;importxiangya.domain.PatientInfo;publicclassPatientInfoDaoImplextendsHibernateDaoSupportimplementsPatientInfoDao{privateLobHandlerlobHandler;publicLobHandlergetLobHandler(){returnlobHandler;}publicvoidsetLobHandler(LobHandlerlobHandler){this.lobHandler=lobHandler;}publicPatientInfoDaoImpl(){//TODOAuto-generatedconstructorstub}@Overridepublicvoidsave(PatientInfopatientInfo){//TODOAuto-generatedmethodstubSystem.out.println("开始保存");getHibernateTemplate().save(patientInfo);System.out.println("结束保存");}@Overridepublicvoiddelete(PatientInfopatientInfo){//TODOAuto-generatedmethodstubSystem.out.println("进入dao层,删除");getHibernateTemplate().delete(patientInfo);}@Overridepublicvoidupdate(PatientInfopatientInfo){//TODOAuto-generatedmethodstubSystem.out.println("进入dao层,更新");getHibernateTemplate().update(patientInfo);}@SuppressWarnings("unchecked")@OverridepublicList<PatientInfo>findByPatientId(StringpatientId){//TODOAuto-generatedmethodstubSystem.out.println("进入dao层,根据病人id查找病人信息");return(List<PatientInfo>)getHibernateTemplate().find("fromPatientInfopwherep.patientId=?",patientId);}@SuppressWarnings("unchecked")@OverridepublicList<PatientInfo>findAllPatient(){//TODOAuto-generatedmethodstubSystem.out.println("进入dao层,查找所有病人信息");return(List<PatientInfo>)getHibernateTemplate().find("fromPatientInfo");}}
解决方案
解决方案二:
不要使用hibernate的事物管理,要使用spring的事物管理<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"></property></bean>
解决方案三:
引用1楼u011564172的回复:
不要使用hibernate的事物管理,要使用spring的事物管理<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"></property></bean>
引用1楼u011564172的回复:
不要使用hibernate的事物管理,要使用spring的事物管理<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"></property></bean>
我看到其他地方都写的要Hibernate管理事务试了下这个还是不行啊。。唉搞了好几天了