上次的说,不配置事务默认是回滚状态是不准确的,是没有设置自动提交。
上次的配置文件用的是hibernate.cfg.xml ,这个配置文件配置如下:
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_session</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property>
spring 配置文件如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
这种只配置了事务管理器,而没有配置事务。
这种方式是提交不了的,需要在hibernate.cfg.xml中加入如下的属性:
<property name="hibernate.connection.autocommit">true</property>
提交就没有问题了
对于这种配置,没有spring的动态代理概念了。
就是在调用bean的时候,是真实的类,而不是代理类:
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
userManager = (UserManager)factory.getBean("userManager");
事务适用于复杂的业务,像单个操作的保存修改,可以不使用事务的。呵呵
下面摘自某位大师的话,呵呵
查了一下,没配datasource,也没配事务的确可以用,
这时候获取的数据库连接是直接通过hibernate的DriverManangerConnectionProvider来提供,spring并没有在其上进行动态代理,也就无法通过TransactionAware接口去设置其FlusMode。说白了这时候,提交不提交取决于两点:
1、hibernate对初始连接的设置(FlushMode)
2、构造conntion是的连接url上是否有autocommit属性的设置
我猜测你的connection url上有可能有autocommit=false之类的设置。
因为在无事务情况下,hibernate不会主动去修改jdbc connection的autocommit属性的。

被折叠的 条评论
为什么被折叠?



