本人初学者,尝试着Spring+SpringMVC+Hibernnate集成测试
在进行持久化操作时获取session我直接使用的sessionFactory.getCurrentSession(),没有用继承HibernateDaoSupport方式来操作
但是我在运行的时候出现了如下错误:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
我在网上也百度了一点,网上都说是没有配置事务的原因,但是确实是配置了,不解~~~ 我很纳闷,在线等解答,请各位前辈指导一下!
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config/> <!-- 读取数据库连接的配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 数据库驱动类配置 --> <property name="driverClass" value="${jdbc.driver}"></property> <!-- url配置 --> <property name="jdbcUrl" value="${jdbc.url}"></property> <!-- 用户名 --> <property name="user" value="${jdbc.username}"></property> <!-- 密码 --> <property name="password" value="${jdbc.password}"></property> <!-- 最小连接池数 --> <property name="minPoolSize" value="2"></property> <!-- 最大连接池数 --> <property name="maxPoolSize" value="20"></property> <!-- 初始化连接数 --> <property name="initialPoolSize" value="3"></property> <!-- 释放连接数 --> <property name="acquireIncrement" value="2"></property> <!-- timeout时间,毫秒 --> <property name="checkoutTimeout" value="10000"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/bdqn/domain/Lianxiren.hbm.xml</value> <value>com/bdqn/domain/SysUsers.hbm.xml</value> </list> </property> </bean> <!-- 配置Hibernate的事务处理 (使用注解的形式)--> <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="HibernateTransactionManager" proxy-target-class="true"/> </beans>
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <!-- 启用springMVC的注解 --> <mvc:annotation-driven/> <context:component-scan base-package="com.bdqn.*"></context:component-scan> <!-- 返回的String视图解析器的配置 --> <!-- 前缀和后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
@Transactional(rollbackFor=Exception.class,readOnly=false)
web.xml中没有配置OpenSessionInViewFilter??
<filter><filter-name>hibernateFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>hibernateFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。