持久層和事務的關係java
dao層脫離事務也能操做數據庫,事務是保證dao層數據操做的完整性(即原子性、一致性、隔離性、持久性,也即所謂的 ACID)node
事務能夠保證一組操做要麼全成功,要麼所有失敗,就是事務是一個不可分割的總體spring
定義beansql
package sping.jdbc;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service(value="jdbcWithoutTM")
public class JdbcWithoutTM {
@Resource(name="jdbcTemplate")
private JdbcTemplate jt;
public void addPerson(){
jt.execute("insert into person(pname) values ('名字')");
}
}數據庫
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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<context:component-scan base-package="sping.jdbc*"></context:component-scan>
<!-- 數據源默認將autoCommit設置爲true -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<context:property-placeholder location="jdbc.properties" />
</beans>測試
junit測試類
package sping.jdbc.test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sping.jdbc.JdbcWithoutTM;
public class SpringJdbcTest {
private static ApplicationContext context;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext(
new String[] { "jdbc.xml" });
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
JdbcWithoutTM jtm=context.getBean("jdbcWithoutTM", JdbcWithoutTM.class);
jtm.addPerson();
}
}url
此程序雖然沒有配置和使用事務控制,可是數據仍是插入到了數據庫。這說明數據庫操做並必定須要事務控制。spa
如今咱們修改下JdbcWithoutTM的插入方法,讓他在數據庫插入操做完成以後手動拋出個異常,看數據是否還能插入。component
public void addPerson(){
jt.execute("insert into person(pname) values ('王江濤')");
throw new RuntimeException();
}
運行程序,查看數據庫,結果是數據仍是插入到了數據庫(事務自動提交),這明顯不符合咱們的要求,說明此操做沒有回滾,形成操做的不完整性,固然,咱們只是一個插入操做,若是咱們在插入操做以後再有個與之相關的數據庫操做,而在兩個操做中間出了個異常,那麼就形成數據不完整了。
因此咱們就要加入事務,而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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<context:component-scan base-package="sping.jdbc*"></context:component-scan>
<!-- 數據源默認將autoCommit設置爲true -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 事務管理配置 若是使用orm框架,則替換爲相應的事務管理器,注意,若是想使用註解性事務,
則需引入cglib-nodep.jar,不是cglib.jar-->
<bean id="myTxManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="myTxManager"></tx:annotation-driven>
<context:property-placeholder location="jdbc.properties" />
</beans>
在插入操做方法上面加上事務註解
@Transactional(propagation=Propagation.REQUIRED)
public void addPerson(){
jt.execute("insert into person(pname) values ('王江濤')");//1
System.out.println(1/0);//2
jt.execute("insert into person(pname) values ('王江濤')");//3
}
再次執行程序,能夠看到,數據庫並無插入任何數據,由於程序在2處出現了異常,因此之前作的全部操做所有回滾了。這就符合咱們的要求啦