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

Spring實戰(zhàn)之使用注解實現聲明式事務操作示例

 更新時間:2020年01月16日 08:38:54   作者:cakincqm  
這篇文章主要介紹了Spring實戰(zhàn)之使用注解實現聲明式事務操作,結合實例形式詳細分析了spring使用注解實現聲明式事務相關配置、接口實現與使用技巧,需要的朋友可以參考下

本文實例講述了Spring實戰(zhàn)之使用注解實現聲明式事務操作。分享給大家供大家參考,具體如下:

一 配置文件

<?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">
   <!-- 定義數據源Bean,使用C3P0數據源實現,并注入數據源的必要信息 -->
   <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"/>
   <!-- 配置一個業(yè)務邏輯Bean -->
   <bean id="newsDao" class="org.crazyit.app.dao.impl.NewsDaoImpl"
      p:ds-ref="dataSource"/>
   <!-- 配置JDBC數據源的局部事務管理器,使用DataSourceTransactionManager 類 -->
   <!-- 該類實現PlatformTransactionManager接口,是針對采用數據源連接的特定實現-->
   <!-- 配置DataSourceTransactionManager時需要依注入DataSource的引用 -->
   <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
      p:dataSource-ref="dataSource"/>
   <!-- 根據Annotation來生成事務代理 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

二 DAO

1 接口

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

2 實現類

package org.crazyit.app.dao.impl;
import javax.sql.DataSource;
import java.sql.Connection;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.*;
import org.crazyit.app.dao.*;
public class NewsDaoImpl implements NewsDao
{
  private DataSource ds;
  public void setDs(DataSource ds)
  {
    this.ds = ds;
  }
  @Transactional(propagation=Propagation.REQUIRED ,
    isolation=Isolation.DEFAULT , timeout=5)
  public void insert(String title, String content)
  {
    JdbcTemplate jt = new JdbcTemplate(ds);
    jt.update("insert into news_inf"
      + " values(null , ? , ?)"
      , title , content);
    // 兩次插入的數據違反唯一鍵約束
    jt.update("insert into news_inf"
      + " values(null , ? , ?)"
      , title , content);
    // 如果沒有事務控制,則第一條記錄可以被插入
    // 如果增加事務控制,將發(fā)現第一條記錄也插不進去。
  }
}

三 測試類

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");
    // 獲取事務代理Bean
    NewsDao dao = (NewsDao)ctx
      .getBean("newsDaoTrans" , NewsDao.class);
    // 執(zhí)行插入操作
    dao.insert("瘋狂Java" , "輕量級Java EE企業(yè)應用實戰(zhàn)");
  }
}

四 測試

數據庫沒生成數據,說明事務生效。

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

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

希望本文所述對大家java程序設計有所幫助。

相關文章

  • spring mvc 使用kaptcha配置生成驗證碼實例

    spring mvc 使用kaptcha配置生成驗證碼實例

    本篇文章主要介紹了spring mvc 使用kaptcha生成驗證碼實例,詳細的介紹了使用Kaptcha 生成驗證碼的步驟,有興趣的可以了解一下
    2017-04-04
  • 解決eclipse啟動tomcat時不能加載web項目的問題

    解決eclipse啟動tomcat時不能加載web項目的問題

    這篇文章主要介紹了解決eclipse啟動tomcat時不能加載web項目的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java實現年獸大作戰(zhàn)游戲詳解

    Java實現年獸大作戰(zhàn)游戲詳解

    春節(jié)要到了,看慣了前端各種小游戲,確實做得很好,很精致。本文將為大家介紹一款java版本的年獸大作戰(zhàn)游戲,感興趣的小伙伴可以試一試
    2022-01-01
  • SpringBoot獲取配置文件中的配置項的常用方式

    SpringBoot獲取配置文件中的配置項的常用方式

    這篇文章主要介紹了SpringBoot獲取配置文件中的配置項的常用方式,并通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11
  • Java字符串常量池示例詳解

    Java字符串常量池示例詳解

    作為最基礎的引用數據類型,Java設計者為 String 提供了字符串常量池以提高其性能,下面這篇文章主要給大家介紹了關于Java字符串常量池的相關資料,需要的朋友可以參考下
    2021-08-08
  • Maven項目分析剔除無用jar引用的方法步驟

    Maven項目分析剔除無用jar引用的方法步驟

    這篇文章主要介紹了Maven項目分析剔除無用jar引用的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • Springboot Logback日志多文件輸出方式(按日期和大小分割)

    Springboot Logback日志多文件輸出方式(按日期和大小分割)

    這篇文章主要介紹了Springboot Logback日志多文件輸出方式(按日期和大小分割),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 基于springboot和redis實現單點登錄

    基于springboot和redis實現單點登錄

    這篇文章主要為大家詳細介紹了基于springboot和redis實現單點登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java使用DateTimeFormatter實現格式化時間

    Java使用DateTimeFormatter實現格式化時間

    這篇文章主要介紹了Java使用DateTimeFormatter實現格式化時間,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • spring @Component注解原理解析

    spring @Component注解原理解析

    這篇文章主要介紹了spring @Component注解原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02

最新評論

霍林郭勒市| 临高县| 麻栗坡县| 鄂尔多斯市| 习水县| 岑巩县| 兴仁县| 忻州市| 隆林| 九龙县| 巴彦淖尔市| 且末县| 且末县| 微博| 信宜市| 罗源县| 凤山县| 靖宇县| 栖霞市| 开原市| 巴青县| 宁阳县| 涪陵区| 金阳县| 安岳县| 桐庐县| 乐安县| 闽侯县| 称多县| 钦州市| 门头沟区| 辽源市| 二手房| 和田市| 明光市| 玛曲县| 义马市| 内黄县| 青神县| 交口县| 贺州市|