spring事务管理

1,建立数据库连接文件jdbc.properties

#数据源配置;

#postgresql
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/testdb
sqlmapxmlpath=classpath:SqlMapConfigForPgsql.xml

#mysql
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8
#sqlmapxmlpath=classpath:SqlMapConfigForPgsql.xml


#jdbc.username=postgres
#jdbc.password=root

# 连接池完成初始化后建立的连接数量
initialPoolSize=0

# 连接池的最小连接数量
minPoolSize=2

# 连接池的最大连接数量
maxPoolSize=10

# 连接的最大空闲时间(单位:秒),当连接空闲超时此时间后,连接将被回收
maxIdleTime=1000


2.配置数据源

	<context:property-placeholder location="classpath:config/properties/jdbc.properties" />
	
	<!-- 配置数据源 -->
	<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}" />
		<property name="maxWait" value="${maxIdleTime}" />
		<property name="maxIdle" value="${maxPoolSize}" />
		<property name="minIdle" value="${minPoolSize}" />
		<property name="initialSize" value="${initialPoolSize}" />
	</bean>

访问文件的配置根据自己的配置

3.配置事务

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">
	
	<bean id="tran" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   		<property name="dataSource" ref="dataSource" />
  	</bean>
  	
  	  <!-- 配置事务特性 -->
	<tx:advice id="tranAdv" transaction-manager="tran">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED" rollback-for="Throwable"/>
			<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" rollback-for="Throwable"/>
			<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" rollback-for="Throwable"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置事务的切入 -->
	<aop:config proxy-target-class="true">
		<!--  配置切入点 -->
		<aop:pointcut expression="execution(* com.test.service.*.impl.*.*(..))" id="pointcut"/>
		<!--  配置切面-->
		<aop:advisor advice-ref="tranAdv" pointcut-ref="pointcut"/>
	</aop:config>
	  
	
  </beans>


OL



你可能感兴趣的:(spring,事务)