hibernate事务提交的问题,为什么每次提交事务都是上一次的操作

J2EE 码拜 8年前 (2016-03-12) 1130次浏览
         学习ssh遇到一个问题,向数据库插入一笔记录,去数据库查看,没有反应,然而当本人再次向数据库插入一笔记录时,数据库显示更新了上一次插入的记录。假如这时候再次向数据库插入一笔记录,数据库显示的是第二次更新的数据,也就是说,本人每插入一笔记录,数据库一直显示上一次的操作有效。假如关闭程序,最后一次的插入记录操作会被丢失。
本人的数据库是mysql,是通过Hibernate Reserve Engineering…自动生成的代码,文件UserDAO.java的代码为:
public class UserDAO extends HibernateDaoSupport implements IUserDAO {
private static final Log log = LogFactory.getLog(UserDAO.class);
// property constants
public static final String USERNAME = “username”;
public static final String PASSWORD = “password”;
protected void initDao() {
// do nothing
}
/* (non-Javadoc)
* @see com.ssh.dao.impl.IUserDAO#save(com.ssh.user.User)
*/
@Override
public void save(User transientInstance) {
log.debug(“saving User instance”);
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
getHibernateTemplate().save(transientInstance);
tx.commit();
log.debug(“save successful”);
} catch (RuntimeException re) {
log.error(“save failed”, re);
throw re;
}
}

}
其中Session session = this.getSession();Transaction tx = session.beginTransaction();这些代码是本人本人添加了,原因是自动生成的代码不会产生事务处理。
hibernate的配置文件hbernate.cfg.xml如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
<!– Generated by MyEclipse Hibernate Tools.                   –>
<hibernate-configuration>
<session-factory>
<property name=”dialect”>
org.hibernate.dialect.MySQLDialect
</property>
<property name=”connection.url”>
jdbc:mysql://localhost:3306/logindemo
</property>
<property name=”connection.username”>root</property>
<property name=”connection.password”>cyj</property>
<property name=”connection.driver_class”>
com.mysql.jdbc.Driver
</property>
<!– <property name=”connection.autocommit”>true</property> –>
<property name=”show_sql”>true</property>
<property name=”myeclipse.connection.profile”>
MySqlConn
</property>
<mapping resource=”com/ssh/user/User.hbm.xml” />
</session-factory>
</hibernate-configuration>
spring配置文件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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd”>
<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
<property name=”configLocation”
value=”classpath:hibernate.cfg.xml”>
</property>
</bean>
<!– 配置事务管理器 –>
<bean id=”transactionManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory”>
<ref local=”sessionFactory”/>
</property>
</bean>

<bean id=”userDAO” class=”com.ssh.dao.impl.UserDAO”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean>
<bean id=”userService” class=”com.ssh.service.impl.UserService”>
<property name=”userDAO” ref=”userDAO”/>
</bean>
<bean name=”baseActionBean” class=”com.ssh.struts.action.BaseAction” scope=”prototype”>
<property name=”userService” ref=”userService”/>
</bean>
</beans>
本人打开调试,每当执行完tx.commit();这句代码,立刻去数据库查看,显示数据库更新了上一次的操作。假如是程序第一次执行插入操作,执行tx.commit();数据库没有任何反应。

解决方案

60

可以尝试在spring里加上事务处理。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明hibernate事务提交的问题,为什么每次提交事务都是上一次的操作
喜欢 (0)
[1034331897@qq.com]
分享 (0)