spring3.1整合hibernate4,事务都配置上了的,但getCurrentSession()仍然获得不到

以下是各配置

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
 
    <!-- Filter 定义 -->
    <!-- Character Encoding filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- Spring MVC Servlet -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
    <!-- open session filter -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
 
    <!-- session超时定义,单位为分钟 -->
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
 
    <!-- Define pages of error -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/error/404.jsp</location>
    </error-page>

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">
            <value>jdbc:mysql://localhost:3306/pannote</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>1234</value>
        </property>
 
        <!-- Max connection numbers in every partition -->
        <property name="maxConnectionsPerPartition" value="15" />
        <!-- Min connection numbers in every partition -->
        <property name="minConnectionsPerPartition" value="1" />
        <!-- Partition numbers,default as 2,min as 1,recommend 3-4,depends on -->
        <property name="partitionCount" value="4" />
        <!-- Everytime the numbers of connection requirement,default as 2 -->
        <property name="acquireIncrement" value="2" />
        <!-- Value of the cache prepared statements,default as 0 -->
        <property name="statementsCacheSize" value="0" />
        <!-- The number of release connection assistant process by every partition -->
        <!-- Default as 3.Your performance will be affected by excessive assistant process unless you need to do a lot work in one connection -->
        <property name="releaseHelperThreads" value="3" />
    </bean>
 
    <!--Define the sessionFactory of hibernate4 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
            </props>
        </property>
        <!-- <property name="mappingResources"> <list> <value>my/hy/pannote/entity/Message.hbm.xml</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="save*" propagation="REQUIRED" read-only="false" />
            <tx:method name="add*" propagation="REQUIRED" read-only="false" />
            <tx:method name="create*" propagation="REQUIRED" read-only="false" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" />
            <tx:method name="turn*" propagation="REQUIRED" read-only="false" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* my.hy.pannote..*.*(..))" />
    </aop:config>

spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<context:component-scan base-package="my.hy.pannote" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
     
    <mvc:annotation-driven />
 
    <mvc:default-servlet-handler />
 
    <!-- 定义首页 -->
    <mvc:view-controller path="/" view-name="redirect:/index" />
 
    <!-- 定义JSP -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

controller

1
2
3
4
5
6
7
8
9
10
11
@RequestMapping(value = "/addMessage", method = RequestMethod.POST)
    public String addMessage(Message message, RedirectAttributes redirectAttributes) {
        try {
            messageService.add(message);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("信息添加错误");
            redirectAttributes.addFlashAttribute("error", "系统错误,请稍后再试");
        }
        return "redirect:/index";
    }

service

1
2
3
4
5
6
7
8
9
10
11
12
@Component
@Transactional
public class MessageService {
 
    @Autowired
    private TestDAO testDAO;
 
    public void add(Message entity) {
        testDAO.save(entity);
    }
 
}

dao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component
public class TestDAO {
     
    @Autowired
    private SessionFactory sessionFactory;
 
    public void save(Message entity) {
        System.out.println("-----"+sessionFactory);
        System.out.println("======"+sessionFactory.getCurrentSession());
        sessionFactory.getCurrentSession().saveOrUpdate(entity);
    }
 
  
  
  
 <div>
     
 
  
  
  
 </div>
}<span style="font-size:9pt;line-height:1.5;"> </span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-----org.hibernate.internal.SessionFactoryImpl@de82eff
org.hibernate.HibernateException: No Session found for current thread
 at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
 at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
 at my.hy.pannote.dao.TestDAO.save(TestDAO.java:18)
 at my.hy.pannote.service.MessageService.add(MessageService.java:23)
 at my.hy.pannote.base.UserBaseController.addMessage(UserBaseController.java:50)
 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.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
 
 
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
 
 
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 
 
 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
 
 
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
 
 
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
 at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
 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)

终于弄好了...原因是openSessionInViewFilter没有过滤请求

根据http://stackoverflow.com/questions/15939932/hibernateexception-no-session-found-for-current-thread-when-calling-service-from

社区里也有这个问题,翻出去了才搜到...http://www.oschina.net/question/659963_87447

我修改了web.xml如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- open session filter -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

被spring和hibernate4逼疯的更多相关文章

  1. Maven下Spring + SpringMvc + Hibernate4 配置实例

    1. 开发环境 IDEA 2. 在pom.xml中配置引用相关的包. <properties> <junit.version>4.10</junit.version> ...

  2. Spring整合hibernate4:事务管理

    Spring整合hibernate4:事务管理 Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTran ...

  3. spring 集成 Hibernate4.3.X org.hibernate.service.jta.platform.spi.JtaPlatform异常

    使用Spring3.2.4集成Hibernate4.3.5时,出现以下异常 Causedby:java.lang.ClassNotFoundException:org.hibernate.servic ...

  4. springMVC整合spring和hibernate4(适合于框架的搭建)

    基础的东西不再详细说明,只在这里说明一下主要的配置文件,如何通过配置文件取得sessionFactory . 步骤: 1:在web.xml中引入springmvc的配置文件springmvc.xml( ...

  5. Spring整合hibernate4:事务管理[转]

    Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作 ...

  6. spring管理hibernate4 transaction getCurrentSession为什么报错?

    hibernate4不支持你用hibernate3的 getcurrentSession,建议你用openSession

  7. Anliven - 如何逼疯你的小伙伴

    如果你也曾为某人某事"发疯发狂,懵逼连连". . 无礼:随意牵扯他人,不了解实际情况,却对他人工作横加点评甚至是指责. 无知:自我感觉良好,自己总是最正确最合理的,除了自己,没人会 ...

  8. [欢乐向]JavaScript之如何逼疯你的同事

    https://javascript.info/ninja-code

  9. 大神note3千元指纹机,这是要逼疯友商吗

    新发现(光山居士).7月20日下午.奇酷公司在北京奥雅会展中心召开公布会,宣布推出首款千元级别的指纹识别机大神Note3.据悉.该型号手机.移动版售价899元.全网通版售1099元,并在16:00開始 ...

随机推荐

  1. surface RT app安装心得

    打开store,然后在键盘输入字母,就出现搜索栏了. 想安装qq,但是输入后找不到软件,原因是我在初始化系统的时候,我的所在地选择的是新加坡,因此找不到软件.在屏幕右下方的setting,然后将所在地 ...

  2. Linux常用命令总结--分布式应用部署与监控

    1 kill所有相关进程ps -ef | grep -i 进程名 | grep -v "grep" | awk '{print $2}' |xargs kill 2 查询当前用户占 ...

  3. Mysql查询比较

    创建一个表: article,书编码:dealer,书店:price ,书的价格. 导入一些数据: INSERT INTO shop VALUES (1,'A',3.45),(1,'B',3.99), ...

  4. Android手势锁实现

    最终效果如下 整体思路 a.自定义了一个RelativeLayout(GestureLockViewGroup)在里面会根据传入的每行的个数,生成多个GestureLockView(就是上面一个个小圈 ...

  5. C#开发微信公众平台-就这么简单(附Demo)(转载)

    转载地址:http://www.cnblogs.com/xishuai/p/3625859.html 写在前面 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件 ...

  6. PHP描述冒泡排序和快速排序算法

    使用PHP描述冒泡排序和快速排序算法,对象可以是一个数组.使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组.写一个二维数组排序算法函数,能够具有通用 ...

  7. Debugging Chromium on Windows

    转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers‎ > ‎How-Tos‎ & ...

  8. Android ImageView图片自适应 (转)

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面)<ImageView     android:id=" ...

  9. Android 饼状图收集

    achartengine 强大的图标绘制工具支持折线图.面积图.散点图.时间图.柱状图.条图.饼图.气泡图.圆环图.范围(高至低)条形图.拨号图/表.立方线图及各种图的结合项目地址:https://c ...

  10. POJ3264Balanced Lineup 线段树练手

    题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include <iostream> #include ...