Spring DataSourceTransactionManager 事务管理

1.spring.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	
    <!-- 获取资源文件 -->
	<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:/com/spring/dataSourceTransactionManager/configuration.properties" />
		
		<!-- 获取多个资源文件 
		 <property name="locations">
		     <list>
		        <value>classpath:/com/zsw/config/jdbc.properties</value>
		     </list>
  		</property>
  		-->
  		<!--  使用location属性定义单个配置文件
        <property name="location">
            <value>classpath:/com/zsw/config/jdbc.properties</value>
        </property>
         -->
  
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	    destroy-method="close">
	    <property name="driverClassName" value="${database.driver}" />
	    <property name="url" value="${database.url}" />
	    <property name="username" value="${database.username}" />
	    <property name="password" value="${database.password}" />
	    <property name="timeBetweenEvictionRunsMillis" value="300000" />
	    <property name="numTestsPerEvictionRun" value="6" />
	    <property name="minEvictableIdleTimeMillis" value="1800000" />
	    <property name="initialSize" value="3" />
	    <property name="maxActive" value="10" />
	    <property name="maxIdle" value="10" />
	    <property name="maxWait" value="5000" />
	    <property name="poolPreparedStatements" value="true" />
	    <property name="maxOpenPreparedStatements" value="100" />
	</bean>
	
	<!-- jndiName来自 dbonfig.properties 配置文件
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:/comp/env/jndi/${jndiName}</value>
		</property>
	</bean>
	-->
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<bean id="productService" class="com.spring.dataSourceTransactionManager.ProductServiceImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
	
	<bean id="receiverService" class="com.spring.dataSourceTransactionManager.ReceiverServiceImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>


	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
    <!-- 采用AOP机制切面管理事务 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="processNotifyTrade" propagation="REQUIRES_NEW" />				
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	<!-- 
		解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义: 第一个 * —— 通配 任意返回值类型 
		第二个 * —— 通配 包com.evan.crm.service下的任意class 
		第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法 第四个 .. —— 通配 方法可以有0个或多个参数
	 -->
 	<aop:config>  
        <aop:pointcut expression="execution(public * com.spring.dataSourceTransactionManager..*.*(..))" id="servicePointcut"/>  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>  
     </aop:config>  
</beans>

 

2.configuration.properties

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1/spring-test?useEncoding=true&characterEncoding=UTF-8
database.username=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true


 

3.ProductService.java

package com.spring.dataSourceTransactionManager;


public interface ProductService {
	
	public void save(String  sql);
	
	public void batch(String[] sql);
}

 

4.ProductServiceImpl.java

package com.spring.dataSourceTransactionManager;

import org.springframework.jdbc.core.JdbcTemplate;


public class ProductServiceImpl implements ProductService {
	
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void save(String sql) {
		jdbcTemplate.execute(sql);
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void batch(String[] sql) {
		jdbcTemplate.batchUpdate(sql);
	}
}

 

5.测试类Test

package com.spring.dataSourceTransactionManager;

import java.sql.SQLException;
import javax.naming.NamingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

	public static void main(String[] args) throws NamingException, SQLException {
		ApplicationContext app = new FileSystemXmlApplicationContext("src/com/spring/dataSourceTransactionManager/spring.xml");
		
		ProductService p = (ProductService)app.getBean("productService");
		
		String[] sql = {"INSERT INTO t_product VALUES (1,'饼干');" , "INSERT INTO t_receiver VALUES (1,null);"};
		
		p.batch(sql);
		
	}
	
}

 

6.Sql

 

DROP TABLE IF EXISTS `t_product`;
CREATE TABLE `t_product` (
  `product_id` bigint(20) NOT NULL auto_increment,
  `product_title` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `t_product` ENABLE KEYS */;
UNLOCK TABLES;

#
# Table structure for table t_receiver
#

DROP TABLE IF EXISTS `t_receiver`;
CREATE TABLE `t_receiver` (
  `receiver_id` bigint(20) NOT NULL auto_increment,
  `receiver_name` varchar(40) NOT NULL default '',
  PRIMARY KEY  (`receiver_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `t_receiver` ENABLE KEYS */;
UNLOCK TABLES;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值