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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?