序文
この記事のサンプルコードのダウンロードアドレス(SQLファイルを含む完全に実行されます。ダウンロード後にデータベースの構成を変更してください):ここをクリックしてダウンロードしてください
いくつかのデータベース操作が全体として制御されており、それらは一緒に成功または失敗します。
Atomicity:トランザクションが不可分な仕事の単位であることを指し、トランザクションでの操作は発生するか、何も発生しません。
一貫性:トランザクションの前後のデータの整合性が一貫している必要があることを意味します。
分離:複数のユーザーがデータベースに同時にアクセスすると、1人のユーザーのトランザクションを他のユーザーのトランザクションによって干渉することはできず、複数の同時トランザクション間のデータを相互に分離する必要があります。
永続性:トランザクションがコミットされると、データベース内のデータの変更は永続的であり、インスタントデータベースの障害はそれに影響を与えないはずです。
-Platform TransactionManagerトランザクションマネージャー(コミット、ロールバックトランザクション)
Springは、さまざまな永続性フレームワーク用のさまざまなプラットフォームトランザクションマネージャーインターフェイスの実装を提供します。のように:
Spring JDBCまたはIbatisを使用してデータを持続するときにDataSourcetransactionManagerを使用する
hibernateTransactionManagerを使用して、hibernate3.0を使用して永続的なデータに使用します
--TransactionDefinitionトランザクション定義情報(分離、伝播、タイムアウト、読み取り専用)
Dirty Reading:あるトランザクションは、別のトランザクションによって書き直されたがまだ提出されていないデータを読み取ります。このデータがロールバックされた場合、読み取りデータは無効です。
繰り返し読み物はありません:同じトランザクションでは、同じデータを複数回読み取ることで返された結果は異なります。
ファンタジーリーディング:1つのトランザクションがいくつかのレコードを読み取ると、別のトランザクションがいくつかのレコードを挿入し、ファンタジーの読みが発生します。後のクエリでは、最初のトランザクションでは、元々利用できなかったレコードがいくつかあります。
トランザクション分離レベル:(5種類)
その中で、MySQLはデフォルトでRepeatable_Read分離レベルを使用しています。 Oracleは、デフォルトでread_committed分離レベルを使用します
トランザクション通信動作:(7種類)
-TransactionStatusトランザクション特定の操作ステータス
a。プログラミングトランザクション管理(Javaプログラミングコントロールに基づく、まれに使用されない) - DEMO1パッケージを参照してください
TransactionTemplateを使用して、複数のDAO操作をカプセル化します
*b。宣言的トランザクション管理(スプリングベースのAOP構成制御)
-TransactionProxyFactoryBeanに基づいています。 (めったに使用されません) - Demo2パッケージを参照してください
強化のためにトランザクション管理を受けている各クラスに対して、TransactionProxyFactoryBeanを構成する必要があります。
- XML構成(頻繁に使用)でベース - Demo3パッケージを参照してください
構成したら、クラスに追加する必要はありません。
アクションがトランザクションを入力するターゲットオブジェクトである場合、<aop:config>要素にプロキシタルゲットクラス= "true"属性を追加する必要があります。その理由は、SpringフレームワークにCGLIBテクノロジーを使用して、トランザクション管理機能を備えたアクションクラスを生成することです。
-Annotations(頻繁に使用される単純な構成)に拠点を置く-Demo4パッケージを参照してください
ApplicationContext.xmlでトランザクションアノテーション構成を有効にします。 (ApplicationContext.xmlで、Beanを定義して、次の要素を追加するだけです)
<bean id = "txmanager"> <Property name = "SessionFactory"> </property> <tx:annotation-driven transaction-manager = "txmanager"/>
ターゲットコンポーネントクラスで@Transactionalを使用します。クラスは、クラスの前またはメソッドの前に定義できます。
- プログラミング
/ *** @description:転送ケースのDAOレイヤーインターフェイス/ ** * * @param in *:転送アカウント * @param Money *:転送金額 */ public void inmoney(string in、double money); }
/ *** @description:転送ケースのDAOレイヤー実装クラス*/ public class courceddaoimplはjdbcdaosupport explmentsアカウント{/ *** @param out*:@param money*:転送金額*/ @Overrideパブリックボイドアウトモニー(弦楽this.getjdbctemplate()。update(sql、money、out); } / *** @param in*:転送アカウント* @param Money*:転送額* / @Override public void inmoney(string in、double money){string sql = "count set money = money+?where name =?"; this.getjdbctemplate()。update(sql、money、in); }} / *** @description:転送ケースのビジネスインターフェース**/ publicインターフェイスアカウントサービス{/ *** @param Out:転送* @param in:@param money:転送金額*/ public void転送(string、string in、double money); } / *** @description:ビジネスレイヤーの実装クラス転送ケースのクラス//トランザクション管理を注入するためのテンプレートプライベートトランザクションテンプレートTransactionTemplate; / ** * @param out *:転送 * @param in *:転送アカウント * @param Money *:転送額 */ @Override public void転送(最終文字列、最終文字列、最終的な2倍のお金){//プロセス中に例外が発生した場合、前の操作を完了できます。後者ができない場合、転送は成功しますが、転送は受信されません。 // accountdao.outmoney(out、money); // int i = 1/0; // accountdao.inmoney(in、money); TransactionTemplate.execute(new TransactionCallbackWithoutresult(){@Override Protected void dointransactionWithoutresult(transactionStatus transactionStatus){accountdao.outmoney(out、money); // int i = 1/0; accountdao.inmoney(in、money)}; } public void setaccountdao(accountdao accountdao){this.accountdao = accountdao; } public void setTransactionTemplate(TransactionTemplate TransactionTemplate){this.transactionTemplate = transactionTemplate; }}ApplicationContext1.xml
<! - 外部プロパティファイルの紹介 - > <コンテキスト:プロパティプレイスホルダーlocation = "classpath:jdbc.properties" /> <! - configure c3p0接続プール - > <bean id = "datasource"> <プロパティ名= "driverclass =" $ {jdbc.driverclass} " /> <プロパティ名= "user" value = "$ {jdbc.username}" /> <property name = "password" value = "$ {jdbc.password}" /> < /bean> <! - ビジネスレイヤークラス - > <bean id = "accountservice"> <property nect = "ref" <Property name = "TransactionTemplate" Ref = "TransactionTemplate" /> < /bean> <! - daoクラスを構成する(simplified、jdbctemplateは自動的に構成されます) - > <bean id = "accountdao"> <プロパティname = "datasource" ref = "ref =" datasource " /> < /bean> < id = "jdbctemplate"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <bean id = "accountdao"> <プロパティ名= "jdbctemplate" ref = "jdbctemplate" /> < /bean> - > <! - ============================================================================================================================================== ====================================================================================================================================== ====================================================================================================================================== ======================================================================================================================================トランザクション管理テンプレートの構成:スプリングが提供するクラストランザクション管理コードを簡素化 - > <bean id = "transactionTemplate"> <プロパティ名= "TransactionManager" ref = "transactionManager"/> </bean>テスト:
@runwith(springjunit4classrunner.class)@contextconfiguration( "classpath:applicationcontext1.xml")public class transactionTest {@Resource(name = "AccountService")private councountservice accountservice; @test public void demo1(){accountservice.transfer( "aaa"、 "bbb"、200d); }}-TransactionProxyFactoryBeanに基づく方法
パブリッククラスaccountserviceimpl accountservice {// dao private councearcondao accountdao; / ** * @param out *:転送 * @param in *:転送account * @param money *:転送額 */ @override public void転送(string、string in、double money){accountdao.outmoney(out、money); // int i = 1/0; accountdao.inmoney(in、money); } public void setaccountdao(accountdao accountdao){this.accountdao = accountdao; }}ApplicationContext2.xml
<! - 外部プロパティファイルの紹介 - > <コンテキスト:プロパティプレイスホルダーlocation = "classpath:jdbc.properties" /> <! - configure c3p0接続プール - > <bean id = "datasource"> <プロパティ名= "driverclass =" $ {jdbc.driverclass} " /> <プロパティ名= "user" value = "$ {jdbc.username}" /> <property name = "password" value = "$ {jdbc.password}" /> < /bean> <! - ビジネスレイヤークラス - > <bean id = "accountservice"> <cultiour "configure" classure " /< (simplied、jdbctemplateを自動的に構成) - > <bean id = "accountdao"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <! - ========================================================================================== ============================================================================================ ========================================================================================== ============================================================================================ - > <! - トランザクションマネージャーの構成 - > <bean id = "transactionmanager"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <! - ビジネスレイヤーのプロキシを構成 - > <bean id = "accountserviceProxy"> < - > <プロパティ名= "TransactionManager" Ref = "TransactionManager"> </Property> <! - inject Transactionプロパティ - > <プロパティ名= "TransactionAttributes"> <Props> <! - Propagation:Propagation:Transaction Propagation Behavion* Isotation:Isotation:トランザクション分離レベルトランザクション - > <prop key = "transfer"> propagation_required </prop> <! - <prop key = "transfagation"> propagation_required、readonly </prop> - > <! - <prop key = "transfer"> propagation_required、readonly </prop> - > < - > </props> </property> </bean>テスト:
@runwith(springjunit4classrunner.class)@contextconfiguration( "classpath:applicationcontext2.xml")public class transactionTest { / ***プロキシクラスは拡張操作を実行するため、拡張操作を実行するため* // @resource(name ")@ressource(namessurce)")アカウントサービスアカウントサービス。 @test public void demo1(){accountservice.transfer( "aaa"、 "bbb"、200d); }}-XML構成に基づいています
パブリッククラスaccountserviceimpl accountservice {// dao private councearcondao accountdao; / ** * @param out *:転送 * @param in *:転送account * @param money *:転送額 */ @override public void転送(string、string in、double money){accountdao.outmoney(out、money); // int i = 1/0; accountdao.inmoney(in、money); } public void setaccountdao(accountdao accountdao){this.accountdao = accountdao; }}ApplicationContext3.xml
<! - 外部プロパティファイルの紹介 - > <コンテキスト:プロパティプレイスホルダーlocation = "classpath:jdbc.properties" /> <! - configure c3p0接続プール - > <bean id = "datasource"> <プロパティ名= "driverclass =" $ {jdbc.driverclass} " /> <プロパティ名= "user" value = "$ {jdbc.username}" /> <property name = "password" value = "$ {jdbc.password}" /> < /bean> <! - ビジネスレイヤークラス - > <bean id = "accountservice"> <cultiour "configure" classure " /< (simplied、jdbctemplateを自動的に構成) - > <bean id = "accountdao"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <! - ================================================================================ ============================================================================== ============================================================================== ============================================================================== ============================================================================== ============================================================================== ============================================================================== ============================================================================== :トランザクション伝播動作分離:トランザクション分離レベル読み取り専用:読み取り専用ロールバック用:どの例外が発生したか:どの例外が発生していませんか? <aop:pointcut expression = "execution(* com.zs.spring.demo3.accountservice+。*(..))" id = "pointcut1"/> <! - 構成セクション - > <aop:aop:asvide advice-ref = "txadvice" pointcut-ref = "pointcut1"/> </aop:config>テスト:
/ *** @Description:Springの宣言的トランザクション管理方法2:AspectJベースのXML構成*/ @RunWith(SpringJunit4ClassRunner.Class)@ContextConfiguration( "ClassPath:ApplicationContext3.xml")Public Class TransactionTest {/ *** = @Resurceの順に就職したため、 「アカウントサービス」)プライベートアカウントサービスアカウントサービス。 @test public void demo1(){accountservice.transfer( "aaa"、 "bbb"、200d); }}- 注釈に基づいています
/ *** @transactional伝播:トランザクション伝播動作分離:トランザクション分離レベル読み取り:読み取り専用* rollbackfor:どの例外が発生したかはロールバックではありません*ロールバック*ロールバックフォークラスネームロールバックは、例外に応じてロールバック*/ @transactional(宣伝= fors = fired readulation. reautagation.edefagation = fired readocation = propagation = propagation = propagation = propagation = readocation = fired。 AccountServiceImplはAccountServiceを実装します{// dao Injecting transfer private councountdaoアカウントダオ; / *** @param out*:cransferアカウント* @param in*:転送account* @param money*:転送額*/ @override public void transfer(string、string in、double money){accountdao.outmoney(out、money); // int i = 1/0; accountdao.inmoney(in、money); } public void setaccountdao(accountdao accountdao){this.accountdao = accountdao; }}ApplicationContext4.xml
<! - 外部プロパティファイルの紹介 - > <コンテキスト:プロパティプレイスホルダーlocation = "classpath:jdbc.properties" /> <! - configure c3p0接続プール - > <bean id = "datasource"> <プロパティ名= "driverclass =" $ {jdbc.driverclass} " /> <プロパティ名= "user" value = "$ {jdbc.username}" /> <property name = "password" value = "$ {jdbc.password}" /> < /bean> <! - ビジネスレイヤークラス - > <bean id = "accountservice"> <cultiour "configure" classure " /< (simplied、jdbctemplateを自動的に構成) - > <bean id = "accountdao"> <プロパティ名= "dataSource" ref = "dataSource" /> < /bean> <! - ========================================================================================== ============================================================================================ ========================================================================================== ============================================================================================トランザクションマネージャーの構成 - > <bean id = "transactionmanager"> <プロパティ名= "dataSource" ref = "dataSource"/> </bean> <! - ターンアノテーショントランザクション - > <tx:annotation-driven transaction-manager = "transactionmanager"/>>テスト:
@runwith(springjunit4classrunner.class)@contextconfiguration( "classpath:applicationcontext4.xml")public class transactionTest { / ***プロキシクラスは拡張操作を実行するため、必ずしも挿入してください。 @test public void demo1(){accountservice.transfer( "aaa"、 "bbb"、200d); }}特定のコードおよびデータベースファイル参照プロジェクトのための完全なコード:
http://xiazai.vevb.com/201805/yuanma/spring-transaction_jb51.rar
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。