spring mysql自动提交事务_Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)...

在本文中,我们将探讨一个Spring 3.2.1与Hibernate 3.3.2集成的示例,其中数据库操作在没有显式事务管理的情况下成功提交。在Config Dao类中,通过HibernateCallback接口更新Config表,尽管autoCommit为true,但数据仍然被更新。这表明在Spring不配置事务管理器的情况下,Hibernate的默认行为可能导致事务隐式提交。
摘要由CSDN通过智能技术生成

数据库为MySQL,Spring版本3.2.1,Hibernate版本3.3.2。

Spring配置文件:

com/zzj/entity/Config.hbm.xml

hibernate.dialect=org.hibernate.dialect.MySQLDialect

hibernate.show_sql=true

实体类:

package com.zzj.entity;

public class Config {

private Integer id;

private String name;

private String value;

public Config() {

}

public Config(Integer id, String name, String value) {

this.id = id;

this.name = name;

this.value = value;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

@Override

public String toString() {

return "Config [id=" + id + ", name=" + name + ", value=" + value + "]";

}

}Hibernate映射文件:

Dao类:

package com.zzj.dao;

import java.sql.SQLException;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class ConfigDao extends HibernateDaoSupport {

public void save(){

getHibernateTemplate().execute(new HibernateCallback() {

@SuppressWarnings("deprecation")

@Override

public Integer doInHibernate(Session session) throws HibernateException, SQLException {

System.out.println("autoCommit:" + session.connection().getAutoCommit());

Query query = session.createQuery("update Config set value = :value where name = :name");

query.setString("name", "Tom");

query.setString("value", "11");

return query.executeUpdate();

}

});

}

}测试类:

package com.zzj;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zzj.dao.ConfigDao;

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-common.xml");

ConfigDao configDao = (ConfigDao) context.getBean("configDao");

configDao.save();

}

}

Spring没有配置事务管理器,Dao类中也没有开启和提交事务的操作,事务也提交了! 注意:Dao类中输出的autoCommit为true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值