首页 > 代码库 > Exception

Exception

配置Spring4 + Hibernate4

遇到的问题:

  

Messages:
  • No Session found for current thread
File:org/springframework/orm/hibernate4/SpringSessionContext.java
Line number:106

Exception:

org.hibernate.HibernateException: No Session found for current thread

    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)    com.venom.dao.impl.BaseDaoImpl.getCurrentSession(BaseDaoImpl.java:40)    com.venom.dao.impl.BaseDaoImpl.findByProperty(BaseDaoImpl.java:135)    com.venom.dao.impl.UserDaoImpl.findByUserName(UserDaoImpl.java:15)    com.venom.service.impl.UserServiceImpl.getUserByUserName(UserServiceImpl.java:18)    com.venom.action.LoginAction.login(LoginAction.java:26)    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    java.lang.reflect.Method.invoke(Method.java:606)    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)    org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2442)    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2431)    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)    java.lang.Thread.run(Thread.java:745)

applicationContext.xml配置

<?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:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    <!-- 自动搜索指定包及其子包下的所有Bean类 -->    <context:component-scan base-package="com.venom" />    <!-- <bean id="loginAction" class="com.venom.action.LoginAction"></bean> -->    <bean        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <value>classpath:dbconfig.properties</value>        </property>    </bean>    <bean id="dataSource" destroy-method="close"        class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- jdbc驱动类 -->        <property name="driverClass" value="${jdbc.driverClass}" />        <!-- 数据库连接 -->        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />        <!-- 用户 -->        <property name="user" value="${jdbc.user}" />        <!-- 密码 -->        <property name="password" value="${jdbc.password}" />        <!-- 连接池最小数 -->        <property name="minPoolSize" value="${jdbc.miniPoolSize}" />        <!-- 连接最大数 -->        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />        <!-- 初始化连接数 -->        <property name="initialPoolSize" value="${jdbc.initialPoolSize}" />        <!--最大空闲时间内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->        <property name="maxIdleTime" value="${jdbc.maxIdleTime}" />        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->        <property name="acquireIncrement" value="${jdbc.acquireIncrement}" />        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->        <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />        <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->        <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" />        <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->        <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" />        <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试             使用。Default: null -->        <property name="automaticTestTable" value="${jdbc.automaticTestTable}" />        <!-- 空闲连接测试时间间隔 -->        <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />        <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default:             0 -->        <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" />    </bean>    <!-- session工厂 -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="dataSource" />        </property>        <property name="configLocation" value="classpath:hibernate.cfg.xml" />        <!-- 自动扫描注解方式配置的hibernate类文件 -->        <property name="packagesToScan">            <list>                <value>com.venom.pojo</value>            </list>        </property>    </bean>    <!-- 配置事务管理器 -->    <bean id="transactionManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- 配置事务通知属性 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <!-- 定义事务传播属性 -->        <tx:attributes>            <tx:method name="insert*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="edit*" propagation="REQUIRED" />            <tx:method name="save*" propagation="REQUIRED" />            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="new*" propagation="REQUIRED" />            <tx:method name="set*" propagation="REQUIRED" />            <tx:method name="remove*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="change*" propagation="REQUIRED" />            <tx:method name="get*" propagation="REQUIRED" read-only="true" />            <tx:method name="find*" propagation="REQUIRED" read-only="true" />            <tx:method name="load*" propagation="REQUIRED" read-only="true" />            <tx:method name="*" propagation="REQUIRED" read-only="true" />        </tx:attributes>    </tx:advice>    </beans>

在applicationContext.xml最后加上:

<aop:config>        <aop:pointcut id="serviceOperation"            expression="execution(* com.venom.service..*.*(..))" />        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />    </aop:config>

事务配置没配完整:

参考:http://www.oschina.net/question/659202_86641

 

Exception