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

scoredhigh 2016-07-08 05:21:32
学习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();数据库没有任何反应。
...全文
293 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
scoredhigh 2016-07-09
  • 打赏
  • 举报
回复
引用 2 楼 moon__stone888888 的回复:
可以尝试在spring里加上事务处理。
嗯,是这个理
moon__stone888888 2016-07-08
  • 打赏
  • 举报
回复
可以尝试在spring里加上事务处理。
moon__stone888888 2016-07-08
  • 打赏
  • 举报
回复
UserDAO类是继承 HibernateDaoSupport类的, HibernateDaoSupport级别最好不要在里面加事务处理。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧