Spring框架事务管理之二:事务管理器与事务API的配置
来源:互联网 发布:发现者行车记录仪淘宝 编辑:程序博客网 时间:2024/06/06 16:31
本文介绍针对JDBC、Hibernate和JTA等事务API,Spring框架中如何进行XML配置。
1. 基于JDBC事务API的Spring XML配置
JDBC事务API依赖于具体的数据源,所以首先要在Spring的XML配置文件中设置数据源如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /></bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/></bean>
2.基于Hibernate事务API的Spring XML配置
Hibernate事务API也依赖于具体的数据源,这被作为Hibernate SessionFactory的参数之一,用于创建Hibernate Session。所以Spring框架提供了特别的SessionFactory如下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=${hibernate.dialect} </value> </property></bean>
定义SessionFactory还需要其他参数。
声明事务时只要引用SessionFactory如下:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /></bean>
补充:即使使用了Hibernate,Spring应用中仍然可以不使用Hibernate的事务API,而使用JTA事务API。
3.基于JTA事务API的Spring XML配置
JTA事务API依赖于JavaEE容器提供的数据源,这往往是通过JNDI获得的,所以无需在Spring的XML配置文件中为JTA事务API设置数据源,但是必须为其指明JNDI的URL,示例如下:
<?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:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> <!-- other <bean/> definitions here --></beans>
1 0
- Spring框架事务管理之二:事务管理器与事务API的配置
- Spring的事务 之 9.2 事务管理器
- Spring框架事务管理之一:JavaEE事务与Spring事务
- spring之事务管理器配置实例
- spring的事务管理器的配置
- spring配置事务管理器,事务配置
- Spring的事务管理器
- Spring的事务管理器
- Spring的事务管理器
- spring配置可以选择的事务管理器
- spring的annotation-driven配置事务管理器
- Spring事务管理器的配置和使用
- Spring框架事务管理之三:Spring声明式事务
- Spring框架事务管理之四:Spring编程式事务
- Spring框架--初探事务管理器
- Spring学习-29:Spring中的事务管理之事务开发常用API的详解
- spring学习之事务管理器
- spring事务管理二:spring事务抽象
- MongoDB基本命令用
- Linux上如何查看Deb和RPM软件包的更新日志
- 编写自己的代码生成工具一:介绍及定义配置文件
- 关于block的强弱引用问题
- C++ new失败的处理
- Spring框架事务管理之二:事务管理器与事务API的配置
- hadoop eclipse plugin windows下载集合
- Android Studio的Configuring高级配置----》屌炸了
- 如何解决Android 5.0中出现的警告:Service Intent must be explicit
- 编写自己的代码生成工具二:解析配置文件
- Windows下编译fast rcnn
- 从数组中找出一对元素,其和是一个给定的目标数字。假设数组中只存在一个符合要求的数值对,返回这些数值的下标
- 编写自己的代码生成工具三:代码生成组织者
- xUtils介绍