最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作示例

 更新時(shí)間:2020年01月15日 09:43:05   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作,結(jié)合實(shí)例形式分析了spring使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)相關(guān)配置、接口設(shè)置與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-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"/>
   <!-- 為業(yè)務(wù)邏輯Bean配置事務(wù)代理
      transactionManager用于為配置事務(wù)代理注入所需的事務(wù)管理器
      target用于指定為哪個(gè)Bean配置事務(wù)代理 -->
   <bean id="newsDaoTrans" class=
   "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
      p:transactionManager-ref="transactionManager"
      p:target-ref="newsDao">
      <!-- 指定事務(wù)屬性 -->
      <property name="transactionAttributes">
        <props>
           <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
      </property>
   </bean>
</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);
    // 如果沒有事務(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("newsDaoTrans" , NewsDao.class);
    // 執(zhí)行插入操作
    dao.insert("瘋狂Java" , "輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)");
  }
}

四 測(cè)試

數(shù)據(jù)庫(kù)中無(wú)數(shù)據(jù),說明事務(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.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
     at com.sun.proxy.$Proxy4.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)
     ... 18 more

更多關(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)文章

  • Java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)哈希表的分離鏈接法

    Java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)哈希表的分離鏈接法

    今天給大家?guī)?lái)的是關(guān)于Java數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),文章圍繞著Java哈希表的分離鏈接法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java設(shè)計(jì)模式之解釋器模式

    Java設(shè)計(jì)模式之解釋器模式

    這篇文章介紹了Java設(shè)計(jì)模式之解釋器模式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • 如何在maven本地倉(cāng)庫(kù)中添加oracle的jdbc驅(qū)動(dòng)

    如何在maven本地倉(cāng)庫(kù)中添加oracle的jdbc驅(qū)動(dòng)

    文章介紹了在Maven項(xiàng)目中添加Oracle數(shù)據(jù)庫(kù)驅(qū)動(dòng)ojdbc5時(shí)遇到的問題以及解決問題的兩種方法,方法一為簡(jiǎn)單粗暴,但沒有體現(xiàn)Maven倉(cāng)庫(kù)的作用,需要手動(dòng)管理jar包,方法二為在Maven本地倉(cāng)庫(kù)中添加Oracle的JDBC驅(qū)動(dòng),過程較為繁瑣,但配置一次后可以多次使用
    2024-11-11
  • Java從零編寫汽車租賃系統(tǒng)全程分析

    Java從零編寫汽車租賃系統(tǒng)全程分析

    這篇文章介紹了Java實(shí)現(xiàn)汽車租賃系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • SpringBoot對(duì)不同Bean注解的區(qū)別和使用場(chǎng)景說明

    SpringBoot對(duì)不同Bean注解的區(qū)別和使用場(chǎng)景說明

    這篇文章主要介紹了SpringBoot對(duì)不同Bean注解的區(qū)別和使用場(chǎng)景說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • elasticsearch中term與match的區(qū)別講解

    elasticsearch中term與match的區(qū)別講解

    今天小編就為大家分享一篇關(guān)于elasticsearch中term與match的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • Java?shiro安全框架使用介紹

    Java?shiro安全框架使用介紹

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會(huì)發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會(huì)出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Shiro框架的使用
    2022-08-08
  • Scala方法與函數(shù)使用和定義詳解

    Scala方法與函數(shù)使用和定義詳解

    這個(gè)章節(jié)會(huì)很燒腦,需要認(rèn)真研讀,我會(huì)盡量寫的詳細(xì)一些。 方法和函數(shù),看似是兩個(gè)概念,其實(shí)他嚴(yán)格來(lái)說也是兩個(gè)概念,但我們大可以理解成是同一個(gè)概念,在使用時(shí)只有語(yǔ)法上的細(xì)微差別,是很類似的,都理解為function即可
    2022-12-12
  • Spring Boot如何獲取maven打包時(shí)間

    Spring Boot如何獲取maven打包時(shí)間

    這篇文章主要介紹了Spring Boot如何獲取maven打包時(shí)間,首先引入maven打包插件,本文分步驟給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2024-03-03
  • SpringBoot中整合消息服務(wù)組件的方法

    SpringBoot中整合消息服務(wù)組件的方法

    本文介紹了消息服務(wù)組件的基本概念,以及如何在SpringBoot中整合常見的消息服務(wù)組件,如ActiveMQ、RabbitMQ和Kafka,我們探討整合消息服務(wù)組件在實(shí)際應(yīng)用場(chǎng)景中的優(yōu)勢(shì),感興趣的朋友跟隨小編一起看看吧
    2023-07-07

最新評(píng)論

离岛区| 万全县| 宁阳县| 秦皇岛市| 南开区| 塘沽区| 长阳| 开平市| 庆元县| 崇义县| 武安市| 顺昌县| 通州区| 海晏县| 枣强县| 南岸区| 花莲县| 新巴尔虎左旗| 龙南县| 石城县| 成都市| 名山县| 沙田区| 牟定县| 乌鲁木齐县| 万盛区| 柯坪县| 宝山区| 永定县| 边坝县| 龙海市| 太保市| 綦江县| 天水市| 河曲县| 莆田市| 文水县| 柞水县| 黄山市| 普兰县| 息烽县|