用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);
}
}