开发者社区> 问答> 正文

Spring+SpringMVC+Hibernnate 获取Session报错,急!?报错

本人初学者,尝试着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>



spring-servlet.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: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)

展开
收起
爱吃鱼的程序员 2020-06-22 11:47:05 548 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    <tx:adviceid="txAdvice"transaction-manager="txManager"><!--thetransactionalsemantics...--><tx:attributes><!--allmethodsstartingwith'*'areread-only--><tx:methodname="get*"read-only="true"/><!--othermethodsusethedefaulttransactionsettings(seebelow)--><tx:methodname="add*"propagation="REQUIRED"/><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="*"read-only="true"/></tx:attributes></tx:advice><aop:configproxy-target-class="true"><aop:advisoradvice-ref="txAdvice"pointcut="execution(*com.name.project..*.*(..))"/></aop:config>



    你这种事务,我也试过了,一样的报错信息

    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>



    回复 @温鹏:我可能还是有点不是很明白,我再看看。谢谢您了回复 @赵运达:你不是已经用了注解的事务了嘛,配置就不需要了。回复 @温鹏:那我刚才把applicationContext.xml中的事务配置都注注释掉也是OK的,那现在hibernate的事务还是由spring控制的吗,感觉不是的了呢?回复 @赵运达:当请求来的时候,在此filter中打开hibernate的session并绑定到当前线程果然是这个的原因,这我之前没有配置过这个filter,请问这是什么配置,什么作用的?谢谢前辈
    2020-06-22 11:47:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多