spring 事务管理
pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.8-dmr</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> </dependencies>
services.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--<bean id="steelAxe" class="org.mythsky.springdemo.SteelAxe"></bean>--> <!--<bean id="chinese" class="org.mythsky.springdemo.Chinese" init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"></bean>--> <!--<bean class="org.mythsky.springdemo.MyBeanPostProcessor"></bean>--> <!--<bean class="org.mythsky.springdemo.MyBeanFactoryPostProcessor"></bean>--> <!--<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">--> <!--<property name="locations">--> <!--<list>--> <!--<value>dbconn.properties</value>--> <!--</list>--> <!--</property>--> <!--</bean>--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}" p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"></bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"></bean> <bean id="newsDao" class="org.mythsky.springtransaction.NewsDaoImpl" p:ds-ref="dataSource"></bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="5"></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="myPointcut" expression="execution(* org.mythsky.springtransaction.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor> </aop:config> <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"--> <!--p:dataSource-ref="dataSource">--> <!--<property name="annotatedClasses">--> <!--<list>--> <!--<value>org.mythsky.springtransaction.User</value>--> <!--</list>--> <!--</property>--> <!--<property name="hibernateProperties">--> <!--<props>--> <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>--> <!--<prop key="hibernate.hbm2ddl.auto">update</prop>--> <!--</props>--> <!--</property>--> <!--</bean>--> <!--<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"--> <!--p:sessionFactory-ref="sessionFactory"></bean>--> <!--<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">--> <!--<property name="locations">--> <!--<list>--> <!--<value>db.properties</value>--> <!--</list>--> <!--</property>--> <!--</bean>--> <!--<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"></bean>--> <context:property-placeholder location="dbconn.properties"></context:property-placeholder> <!--<context:property-override location="db.properties"></context:property-override>--> <context:component-scan base-package="org.mythsky.springtransaction"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--<bean id="myAspect" class="org.mythsky.springaopdemo.AuthAspect"></bean>--> <!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>--> </beans>
NewsDao
package org.mythsky.springtransaction; public interface NewsDao { public void insert(String title, String content); }
NewsDaoImpl
package org.mythsky.springtransaction; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; public class NewsDaoImpl implements NewsDao { private DataSource ds; public void setDs(DataSource ds) { this.ds = ds; } public void insert(String title, String content) { JdbcTemplate jt=new JdbcTemplate(ds); jt.update("INSERT INTO news VALUES (1 ,?,?)",title,content); jt.update("INSERT INTO news VALUES (1 ,?,?)",title,content); } }
SpringTest
package org.mythsky.springtransaction; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml"); NewsDao dao=ctx.getBean("newsDao",NewsDao.class); dao.insert("hello","world"); } }
创建表
CREATE TABLE `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
因为开启了事务,所以数据库并没有插入数据,如果取消事务,会插入第一条数据,第二条会报错。