欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring實(shí)戰(zhàn)之使用XML方式管理聲明式事務(wù)操作示例

 更新時(shí)間:2020年01月15日 09:07:51   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用XML方式管理聲明式事務(wù)操作,結(jié)合實(shí)例形式詳細(xì)分析了Spring XML方式管理聲明式事務(wù)具體步驟、配置、接口及使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(zhàn)之使用XML方式管理聲明式事務(wù)操作。分享給大家供大家參考,具體如下:

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:p="http://www.springframework.org/schema/p"
   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-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
   <!-- 定義數(shù)據(jù)源Bean,使用C3P0數(shù)據(jù)源實(shí)現(xiàn),并注入數(shù)據(jù)源的必要信息 -->
   <bean id="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close" p:driverClass="com.mysql.jdbc.Driver"
      p:jdbcUrl="jdbc:mysql://localhost/spring" p:user="root"
      p:password="32147" p:maxPoolSize="40" p:minPoolSize="2"
      p:initialPoolSize="2" p:maxIdleTime="30" />
   <!-- 配置JDBC數(shù)據(jù)源的局部事務(wù)管理器,使用DataSourceTransactionManager 類 -->
   <!-- 該類實(shí)現(xiàn)PlatformTransactionManager接口,是針對(duì)采用數(shù)據(jù)源連接的特定實(shí)現(xiàn) -->
   <!-- 配置DataSourceTransactionManager時(shí)需要依注入DataSource的引用 -->
   <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
      p:dataSource-ref="dataSource" />
   <!-- 配置一個(gè)業(yè)務(wù)邏輯Bean -->
   <bean id="newsDao" class="org.crazyit.app.dao.impl.NewsDaoImpl"
      p:ds-ref="dataSource" />
   <!-- 配置事務(wù)增強(qiáng)處理Bean,指定事務(wù)管理器 -->
   <tx:advice id="txAdvice"
      transaction-manager="transactionManager">
      <!-- 用于配置詳細(xì)的事務(wù)語(yǔ)義 -->
      <tx:attributes>
        <!-- 所有以'get'開(kāi)頭的方法是read-only的 -->
        <tx:method name="get*" read-only="true" />
        <!-- 其他方法使用默認(rèn)的事務(wù)設(shè)置,指定超時(shí)時(shí)長(zhǎng)為5秒 -->
        <tx:method name="*" isolation="DEFAULT"
           propagation="REQUIRED" timeout="5" />
      </tx:attributes>
   </tx:advice>
   <!-- AOP配置的元素 -->
   <aop:config>
      <!-- 配置一個(gè)切入點(diǎn),匹配org.crazyit.app.dao.impl包下 所有以Impl結(jié)尾的類里、所有方法的執(zhí)行 -->
      <aop:pointcut id="myPointcut"
        expression="execution(* org.crazyit.app.dao.impl.*Impl.*(..))" />
      <!-- 指定在myPointcut切入點(diǎn)應(yīng)用txAdvice事務(wù)增強(qiáng)處理 -->
      <aop:advisor advice-ref="txAdvice"
        pointcut-ref="myPointcut" />
   </aop:config>
</beans>

二 DAO

1 接口

package org.crazyit.app.dao;
public interface NewsDao
{
   public void insert(String title, String content);
}

2 實(shí)現(xiàn)類

package org.crazyit.app.dao.impl;
import javax.sql.DataSource;
import java.sql.Connection;
import org.springframework.jdbc.core.JdbcTemplate;
import org.crazyit.app.dao.*;
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_inf"
      + " values(null , ? , ?)"
      , title , content);
    // 兩次插入的數(shù)據(jù)違反唯一鍵約束
    jt.update("insert into news_inf"
      + " values(null , ? , ?)"
      , title , content);
    // 如果沒(méi)有事務(wù)控制,則第一條記錄可以被插入
    // 如果增加事務(wù)控制,將發(fā)現(xiàn)第一條記錄也插不進(jìn)去。
  }
}

三 測(cè)試類

package lee;
import org.springframework.context.support.*;
import org.springframework.context.*;
import org.crazyit.app.dao.*;
public class SpringTest
{
  public static void main(String[] args)
  {
    // 創(chuàng)建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 獲取事務(wù)代理Bean
    NewsDao dao = (NewsDao)ctx
      .getBean("newsDao" , NewsDao.class);
    // 執(zhí)行插入操作
    dao.insert("瘋狂Java" , "輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)");
  }
}

四 測(cè)試結(jié)果

數(shù)據(jù)沒(méi)插入到數(shù)據(jù)庫(kù),說(shuō)明事務(wù)生效

Exception in thread "main"  org.springframework.dao.DuplicateKeyException:  PreparedStatementCallback; SQL [insert into news_inf  values(null , ? , ?)]; Duplicate entry '瘋狂Java' for key  'news_title'; nested exception is  com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '瘋狂Java' for key 'news_title'
     at  org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
     at  org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
     at  org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
     at  org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909)
     at  org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970)
     at  org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980)
     at  org.crazyit.app.dao.impl.NewsDaoImpl.insert(NewsDaoImpl.java:33)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native  Method)
     at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:498)
     at  org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
     at  org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
     at  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
     at  org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
     at  org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
     at  org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
     at  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
     at  org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
     at  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
     at  org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
     at com.sun.proxy.$Proxy5.insert(Unknown Source)
     at lee.SpringTest.main(SpringTest.java:28)
Caused by:  com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '瘋狂Java' for key 'news_title'
     at  sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native  Method)
     at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
     at  sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     at  java.lang.reflect.Constructor.newInstance(Constructor.java:423)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
     at com.mysql.jdbc.Util.getInstance(Util.java:384)
     at  com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
     at  com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
     at  com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
     at  com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
     at  com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
     at  com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
     at  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
     at  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
     at  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
     at  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
     at  org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:916)
     at  org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:909)
     at  org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • IDEA插件推薦之Maven-Helper的教程圖解

    IDEA插件推薦之Maven-Helper的教程圖解

    這篇文章主要介紹了IDEA插件推薦之Maven-Helper的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考
    2020-07-07
  • SpringBoot集成WebSocket【基于純H5】進(jìn)行點(diǎn)對(duì)點(diǎn)[一對(duì)一]和廣播[一對(duì)多]實(shí)時(shí)推送

    SpringBoot集成WebSocket【基于純H5】進(jìn)行點(diǎn)對(duì)點(diǎn)[一對(duì)一]和廣播[一對(duì)多]實(shí)時(shí)推送

    這篇文章主要介紹了SpringBoot集成WebSocket【基于純H5】進(jìn)行點(diǎn)對(duì)點(diǎn)[一對(duì)一]和廣播[一對(duì)多]實(shí)時(shí)推送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Java開(kāi)發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    Java開(kāi)發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了Java開(kāi)發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • java面試題解LeetCode27二叉樹的鏡像實(shí)例

    java面試題解LeetCode27二叉樹的鏡像實(shí)例

    這篇文章主要為大家介紹了java面試題解LeetCode27二叉樹的鏡像實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • SpringBoot 自定義+動(dòng)態(tài)切換數(shù)據(jù)源教程

    SpringBoot 自定義+動(dòng)態(tài)切換數(shù)據(jù)源教程

    這篇文章主要介紹了SpringBoot 自定義+動(dòng)態(tài)切換數(shù)據(jù)源教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解Java 微服務(wù)架構(gòu)

    詳解Java 微服務(wù)架構(gòu)

    這篇文章主要介紹了Java 微服務(wù)架構(gòu)的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02
  • 使用maven war包打包去除jar包瘦身

    使用maven war包打包去除jar包瘦身

    這篇文章主要介紹了使用maven war包打包去除jar包瘦身操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之貪心算法

    Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之貪心算法

    我們可能在好多地方都會(huì)聽(tīng)到貪心算法這一概念,并且它的算法思想也比較簡(jiǎn)單就是說(shuō)算法只保證局部最優(yōu),進(jìn)而達(dá)到全局最優(yōu)。但我們實(shí)際編程的過(guò)程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問(wèn)題必須滿足貪心選擇性質(zhì)
    2022-02-02
  • 劍指Offer之Java算法習(xí)題精講二叉樹專項(xiàng)解析

    劍指Offer之Java算法習(xí)題精講二叉樹專項(xiàng)解析

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • Springboot Thymeleaf數(shù)字對(duì)象使用方法

    Springboot Thymeleaf數(shù)字對(duì)象使用方法

    這篇文章主要介紹了Springboot Thymeleaf數(shù)字對(duì)象使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2007-09-09

最新評(píng)論