spring 事务配置二

上次的说,不配置事务默认是回滚状态是不准确的,是没有设置自动提交。

上次的配置文件用的是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属性的。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值