我见过几个类似的问题,但没有一个建议的解决方案对我有帮助。
我们正在将应用程序迁移到Spring 4.0.5 + Hibernate 4.3.5 + C3P0 9.2.1,我们遇到了事务/会话的问题。
的ApplicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.servicetonic.sdesk" />
<aop:aspectj-autoproxy />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="contextApplicationContextProvider" class="com.servicetonic.sdesk.util.context.ApplicationContextProvider"></bean>
<bean id="dataSource" name="sDeskBD"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" >
<!-- Driver -->
<property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>
<property name="jdbcUrl"><value>jdbc:mysql://localhost:3306/dbst</value></property>
<property name="user"><value>root</value></property>
<property name="password"><value>AzulBlanco09</value></property>
<!-- Tamanyo del pool -->
<property name="initialPoolSize"><value>1</value></property>
<property name="minPoolSize"><value>1</value></property>
<property name="acquireIncrement"><value>1</value></property>
<property name="maxPoolSize"><value>50</value></property>
<!-- Vigencia de las conexiones-->
<property name="maxIdleTime"><value>7200</value></property>
<!-- Test de las conexiones -->
<property name="automaticTestTable"><value>STTestTable</value></property>
<property name="idleConnectionTestPeriod"><value>300</value></property>
<property name="testConnectionOnCheckin"><value>true</value></property>
<!-- Ejecucion de sentencias -->
<property name="maxStatements"><value>0</value></property>
<property name="maxStatementsPerConnection"><value>0</value></property>
<!-- Recuperacion de caidas de BBDD-->
<property name="acquireRetryAttempts"><value>60</value></property>
<property name="acquireRetryDelay"><value>1000</value></property>
<property name="breakAfterAcquireFailure"><value>false</value></property>
<!-- Soporte a TX pendientes -->
<property name="autoCommitOnClose"><value>false</value></property>
<!-- Otras configuraciones -->
<property name="numHelperThreads"><value>5</value></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="/WEB-INF/config/hibernate/hibernate.cfg.xml" />
<property name="packagesToScan" value="com.servicetonic.sdesk" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
Hibernate配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.query.substitutions">true 'Y', false 'N'</property>
<!-- Dialecto -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.use_outer_join">false</property>
<property name="hibernate.session_factory_name">hibernate</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.jdbc.batch_size">30</property>
<!-- Configuracion pool c3p0 -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.timeout">7200</property>
<!-- Mapping -->
<mapping class="com.servicetonic.sdesk.modelo......"/>
.....
</session-factory>
</hibernate-configuration>
DAO实施:
@Repository
public class SpringHibernateDAO implements DAO {
@Autowired
private SessionFactory sessionFactory;
@Transactional(readOnly = true)
public <T> List<T> find(String hql) {
final List<T> entities = getCurrentSession().createQuery(hql).list();
return entities;
}
@Override
public Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
.....
}
使用此配置我收到以下错误:
Exception in thread "Thread-9" org.hibernate.HibernateException: createQuery is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy128.createQuery(Unknown Source)
at com.servicetonic.sdesk.services.dao.impl.SpringHibernateDAO.find(SpringHibernateDAO.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy79.find(Unknown Source)
at com.servicetonic.sdesk.services.tools.impl.PatchServiceImpl.getPatchs(PatchServiceImpl.java:50)
at com.servicetonic.sdesk.services.global.ProcessServicePackRunnable.run(ProcessServicePackRunnable.java:72)
at java.lang.Thread.run(Thread.java:722)
如果我们删除属性hibernate.current_session_context_class = thread那么我们得到以下错误:
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.servicetonic.sdesk.services.dao.impl.SpringHibernateDAO.getCurrentSession(SpringHibernateDAO.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy79.obtenerSesion(Unknown Source)
at com.servicetonic.sdesk.services.jdbc.impl.JdbcServiceImpl.executeQuery(JdbcServiceImpl.java:184)
at com.servicetonic.sdesk.services.global.impl.TranslateServiceImpl.getTraduccionesLV(TranslateServiceImpl.java:104)
at com.servicetonic.sdesk.services.global.impl.TranslateServiceImpl.createTranslateMap(TranslateServiceImpl.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy95.createTranslateMap(Unknown Source)
at com.servicetonic.sdesk.views.beans.application.ApplicationManagedBean.init(ApplicationManagedBean.java:41)
at com.servicetonic.sdesk.views.beans.application.ApplicationManagedBean.<init>(ApplicationManagedBean.java:34)
at com.servicetonic.sdesk.util.sesion.ContextListener.contextInitialized(ContextListener.java:27)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
任何帮助将不胜感激。
感谢。