spring开发_JDBC操作MySQL数据库_使用xml配置事务管理

项目:

http://www.cnblogs.com/hongten/archive/2012/03/09/java_spring_jdbc.html

只需修改:

/spring_1100_spring+jdbc/src/bean.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
10
11 <context:property-placeholder location="classpath:jdbc.properties" />
12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
13 destroy-method="close">
14 <property name="driverClassName" value="${driverClassName}" />
15 <property name="url" value="${url}" />
16 <property name="username" value="${username}" />
17 <property name="password" value="${password}" />
18 <!-- 连接池启动时的初始值 -->
19 <property name="initialSize" value="${initialSize}" />
20 <!-- 连接池的最大值 -->
21 <property name="maxActive" value="${maxActive}" />
22 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
23 <property name="maxIdle" value="${maxIdle}" />
24 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
25 <property name="minIdle" value="${minIdle}" />
26 </bean>
27
28 <bean id="txManager"
29 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
30 <property name="dataSource" ref="dataSource" />
31 </bean>
32 <!-- 使用xml配置事务 --begin-->
33 <aop:config>
34 <aop:pointcut id="transactionPointcut" expression="execution(* com.b510.service..*.*(..))" />
35 <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
36 </aop:config>
37 <tx:advice id="txAdvice" transaction-manager="txManager">
38 <tx:attributes>
39 <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
40 <tx:method name="*" />
41 </tx:attributes>
42 </tx:advice>
43 <!-- 使用xml配置事务 --end -->
44
45 <bean id="personService" class="com.b510.service.impl.PersonServiceBean">
46 <property name="dataSource" ref="dataSource" />
47 </bean>
48 </beans>

运行效果:

 1 2012-3-10 14:30:18 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]; startup date [Sat Mar 10 14:30:18 CST 2012]; root of context hierarchy
3 2012-3-10 14:30:18 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [bean.xml]
5 2012-3-10 14:30:18 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1f3aa07
7 2012-3-10 14:30:18 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
8 信息: Loading properties file from class path resource [jdbc.properties]
9 2012-3-10 14:30:18 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
10 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f3aa07: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,transactionPointcut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,txAdvice,personService]; root of factory hierarchy
11 ++++++++得到所有Person
12 2 TomCat 12 女
13 3 hongten 21 男
14 4 liufang 21 女
15 5 Divide 20 男
16 6 Jone 20 女
17 7 苏东坡 21 男
18 8 苏东坡 21 男
19 ******************
20 2 TomCat 12 女



原文地址:https://www.cnblogs.com/hongten/p/java_spring_jdbc_xml_transaction.html