thebye85 2008-06-13 14:48
浏览 486
已采纳

请教hibernate.connection.autocommit的问题

用Spring整合Hibernate。没有用事务,Spring配置如下:

[code="java"]
class="org.apache.commons.dbcp.BasicDataSource">
value="com.mysql.jdbc.Driver">




<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
           <prop key="hibernate.show_sql">true</prop>
           [color=red]<prop key="hibernate.connection.autocommit">false</prop>[/color]
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>vo/Student.hbm.xml</value>
        </list>
    </property>
</bean>

[/code]

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao{
public void save(Student stu) {
[color=red]this.getHibernateTemplate().save(stu); //问题:为什么上面设置autocommit为false,但这里还是会提交到数据库中[/color]
}
}

[b]问题补充:[/b]
谢谢你的回复。

我用的是MySQL,但我发现即使加个声明式事务,调用getHibernateTemplate().save(stu)方法还是会插入到数据库中。以下是我的事务配置:




<bean id="studentDaoProxy"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
    <property name="transactionManager">
       <ref bean="transactionManager"/>
    </property> 
    <property name="transactionAttributes"> 
        <props> 
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props> 
    </property> 
    <property name="target"> 
        <ref local="studentDao"/>
    </property>
</bean>


<bean id="studentDao" class="vo.StudentDaoImpl">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

这是我的测试类:

public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = (StudentDao)context.getBean("studentDaoProxy");
//添加操作
Student student = new Student();
student.setName("测试");
studentDao.save(student);
}
}

  • 写回答

2条回答 默认 最新

  • iteye_521 2008-06-13 17:35
    关注

    hibernate事务是依赖与数据库事务的,调用save方法后SQL语句已经发送到数据库,所以提交到数据库很正常,如果你应用了事务但是autocommit为false才不会保存到数据库,前提是你的表是InnoDB(支持事务),如果是MyISAM(不支持事务)的话不管你是否应用了事务只要调用save方法数据库中都会存入记录

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)