spring事务环境搭建
生活随笔
收集整理的這篇文章主要介紹了
spring事务环境搭建
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring事務(wù)里面主要是分為兩種事務(wù),一種是叫做編程事務(wù),編程事務(wù)我們叫做手動事務(wù),自己去begin,commit,rollback回滾,這個是編程事務(wù)的,而起我剛才也講過的,聲明事務(wù)本質(zhì)是使用編程事務(wù)和反射機(jī)制包裝起來的,明天我們會手寫一下聲明式事務(wù),你們之前有沒有用過JDBC的,你們看一下,在這個地方,編程事務(wù)實現(xiàn),你們用過JDBC的話就知道,它是需要獲取JDBC的事務(wù)模板之后,然后你才去做begin和commit,我不知道你們有沒有印象,我跟你們講一下,框架會給我們提供一個事務(wù)接口的,這一點我要和你們講一下,本質(zhì)上都是通過編程事務(wù)封裝起來的,在這邊我就講個例子,講個什么例子呢,把環(huán)境搭建起來,搭建一個數(shù)據(jù)庫的環(huán)境,這邊我直接把代碼copy過來,我先把環(huán)境搭建起來CREATE TABLE IF NOT EXISTS `t_users`(`NAME` INT UNSIGNED AUTO_INCREMENT,`age` VARCHAR(100) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/javassist/javassist --><dependency><groupId>javassist</groupId><artifactId>javassist</artifactId><version>3.12.1.GA</version></dependency><!-- 引入Spring-AOP等相關(guān)Jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.1</version></dependency><dependency><groupId>aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.5.3</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.1_2</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency></dependencies></project>
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 這里表示掃包范圍 因為我們是使用注解的,--><context:component-scan base-package="com.learn"></context:component-scan><!-- 這里表示開啟事務(wù)的注解 你如果想要事務(wù)的話,你必須開啟一個事務(wù)注解,--><aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 開啟事物注解 --><!-- 1. 數(shù)據(jù)源對象: C3P0連接池 --><!-- 第一步我們加載C3P0數(shù)據(jù)源 DBCP和C3P0的區(qū)別講一下,數(shù)據(jù)庫的連接池,--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day20"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean><!-- 2. JdbcTemplate工具類實例 --><!-- 這里要引用到我的數(shù)據(jù)源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 3.配置事務(wù) --><bean id="dataSourceTransactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>
</beans>
package com.learn.dao;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;/*** 在UserDao里面我們寫一個什么代碼呢* 是關(guān)于數(shù)據(jù)庫的* * @author Leon.Sun**/
@Repository
public class UserDao {@Autowiredprivate JdbcTemplate jdbcTemplate;/*** 表示我有一個add方法* 有兩個參數(shù)* 叫name和age* * * @param name* @param age*/public void add(String name, Integer age) {String sql = "INSERT INTO t_users(NAME, age) VALUES(?,?);";int updateResult = jdbcTemplate.update(sql, name, age);System.out.println("updateResult:" + updateResult);}}
package com.learn.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.learn.dao.UserDao;
import com.learn.service.UserService;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;public void add() {/*** 假設(shè)20歲*/userDao.add("test001", 20);/*** 我在這邊打個斷點大家看一下* 沒有事務(wù)為什么不提交到數(shù)據(jù)庫里面去呢* 走完之后立即提交* 因為這邊沒有事務(wù)控制的* 這邊我去斷點調(diào)試一下* 這段代碼走完之后數(shù)據(jù)庫里面就有了* 你有了之后后面報個錯* 你已經(jīng)提交到數(shù)據(jù)庫了就回滾不了了* 就會產(chǎn)生數(shù)據(jù)庫不一致的問題* */System.out.println("開始報錯了..........................");/*** 我寫一段代碼int i = 1/0* 這個時候我來問一下你們* 會插入幾條數(shù)據(jù)* test001會成功* test002會失敗* 只有一條成功了* 我們看一下數(shù)據(jù)庫里面* 因為當(dāng)他走到test002的時候* 這個時候已經(jīng)報錯了* 肯定不會往下繼續(xù)執(zhí)行了* 這個程序直接中斷了* 后面的代碼就不用走了* 那這樣的代碼肯定不合理* 我們要用事務(wù)來保證事務(wù)的一致性問題* 是不是應(yīng)該全部失敗* 而不是一個成功一個失敗* 是不是這樣的* 這個代碼肯定有錯* 在這邊我們可以用到事務(wù)* 在這邊我們可以采用事務(wù)了* 把全部整個都做回滾* 怎么做呢* 在這邊我就要詳細(xì)給你們講了* 首先在這邊我要問一下* int i = 1/0;這行走完了之后* 這個數(shù)據(jù)會不會添加到數(shù)據(jù)庫里面去* 你們說一下* int i = 1/0這行走完的時候數(shù)據(jù)會不會添加到數(shù)據(jù)庫里面去* 肯定會的* * */int i = 1/0;/*** 中間打印一個分隔符*/System.out.println("####################################");/*** 再來一個test002是21歲*/userDao.add("test002", 21);}}
package com.learn.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.service.UserService;public class Test001 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");UserService userService = (UserService) applicationContext.getBean("userServiceImpl");userService.add();}}
手寫Spring事務(wù)框架
編程事務(wù)實現(xiàn)
概述
所謂編程式事務(wù)指的是通過編碼方式實現(xiàn)事務(wù),即類似于JDBC編程實現(xiàn)事務(wù)管理。管理使用TransactionTemplate或者
直接使用底層的PlatformTransactionManager。對于編程式事務(wù)管理,spring推薦使用TransactionTemplate。
案例
使用編程事務(wù)實現(xiàn)手動事務(wù)
使用編程事務(wù)實現(xiàn),手動事務(wù) begin、commit、rollback
?
總結(jié)
以上是生活随笔為你收集整理的spring事务环境搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring声明事务与编程事务概述
- 下一篇: 手写spring编程事务