首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring 事宜配置二

2012-10-09 
spring 事务配置二上次的说,不配置事务默认是回滚状态是不准确的,是没有设置自动提交。上次的配置文件用的

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" name="code"><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属性的。

热点排行