具体错误如图所示
错误原因
未正确配置事务。
首先纠正一点,如下配置并不能解决问题。
早在 hinernate4 的 OpenSessionInViewFilter, 就去掉了hibernate3的setFlushMode的方法, 所以在web.xml配置全局变量并不管用
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
Spring 事务原理
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问 时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。
经检查,我的错误原因是没有配置transactionManager
。
解决方式——正确配置 Spring 事务
先给出我的解决方式
1.配置 DataSource
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--这里配置为你自己的数据库信息-->
<property name="url" value="jdbc:mysql://localhost:3306/JSP"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</bean>
2.配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--这里配置为在上一步中设置的dataSource名-->
<property name="dataSource" ref="datasource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.LoginEntity</value>
<value>model.StudentEntity</value>
<value>model.AchievementEntity</value>
<value>model.CourseEntity</value>
</list>
</property>
</bean>
3.配置transactionManager
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!--这里配置为在上一步中设置的sessionFactory名-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
- 配置transactionInterceptor
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!--这里配置为在上一步中设置的transactionManager名-->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<!--设置权限列表,支持通配符-->
<!--此处设置为给所有函数读写权限-->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
5.配置BeanNameAutoProxyCreator
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<!--配置应用transactionInterceptor的 DaoImp的列表,可以使用通配符-->
<value>*DaoImp</value>
</list>
</property>
<property name="interceptorNames">
<list>
<!--这里配置为在上一步中设置的transactionInterceptor名-->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
至此,问题解决。
附上我的spring-config 文件,仅供参考
<?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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/JSP"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.LoginEntity</value>
<value>model.StudentEntity</value>
<value>model.AchievementEntity</value>
<value>model.CourseEntity</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*DaoImp</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!--login-->
<bean id="loginDaoImp" class="dao.imp.LoginDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="loginServiceManage" class="service.imp.LoginServiceManage">
<property name="loginDao" ref="loginDaoImp"/>
</bean>
<bean id="loginAction" class="action.LoginAction" scope="prototype">
<property name="loginService" ref="loginServiceManage"/>
</bean>
<!--info-->
<bean id="infoDaoImp" class="dao.imp.InfoDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="infoServiceManage" class="service.imp.InfoServiceManage">
<property name="infoDao" ref="infoDaoImp"/>
</bean>
<bean id="infoAction" class="action.InfoAction">
<property name="infoService" ref="infoServiceManage"/>
</bean>
<!--achievement-->
<bean id="achievementDaoImp" class="dao.imp.AchievementDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="achievementServiceManage" class="service.imp.AchievementServiceManage">
<property name="achievementDao" ref="achievementDaoImp"/>
</bean>
<bean id="achievementAction" class="action.AchievementAction">
<property name="achievementService" ref="achievementServiceManage"/>
</bean>
</beans>
Terry.Li-彬 在他的博客 中给出给多的事务配置方式,本文参考了其文中的一种配置方式。