SpringMVC注釈トランザクション

7082 ワード


<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager" />

1.Hibernateトランザクションマネージャの構成
2.注釈トランザクションのサポートを開始
問題:
1.サービスクラスのメソッドで@Transactional注記を設定しますが、トランザクションは機能しません.つまり、更新操作後に例外が発生し、トランザクションはロールバックされません.
解決:トランザクション構成をdispatcher-servletに移行します.xmlで.エラーは、アプリケーションContextにトランザクション構成を配置したためです.xmlでは、サービスレイヤがdispatcher-servletコンテナに管理されているため、トランザクションは失効します.
トランザクションブロックはアプリケーションContextコンテナにあり、トランザクションが無効になります.
<listener> 

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 

</listener> 


 
知識点:
Springでは、親コンテキスト(親コンテナ)と呼ばれるWebApplicationContextコンテキストが作成され、サーブレットContextに保存されます.keyはWebApplicationContextです.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEの値.
Springが提供するツールクラスを使用してコンテキストオブジェクトを取り出すことができます:WebApplicationContextUtils.getWebApplicationContext(ServletContext);
 
DispatcherServiceletは1つのサーブレットであり、複数を同時に構成することができ、各DispatcherServiceletにはサブコンテキスト(サブコンテナ)と呼ばれる独自のコンテキストオブジェクトがあり、サブコンテキストは親コンテキストのコンテンツにアクセスできますが、親コンテキストはサブコンテキストのコンテンツにアクセスできません.また、keyは「org.springframework.web.servlet.Framework Service.CONTEXT」+サーブレット名であるサーブレットContextにも保存されます.Requestオブジェクトが生成すると、このサブコンテキストオブジェクト(WebApplicationContext)はRequestオブジェクトに保存され、keyはDispatcherServiceletである.class.getName() + ".CONTEXT".
ツールクラスを使用してコンテキストオブジェクトを取り出すことができます:RequestContextUtils.getWebApplicationContext(request);
説明:Springは私たちを制限していません.親子コンテキストを使用する必要があります.私たちは自分でどのように使うかを決めることができます.
http://elf8848.iteye.com/blog/875830/
ソースコードのコンテキスト取得方法のコメント:
1.WebApplicationContextUtils.getWebApplicationContext(サーブレットContext)メソッドコメント:
  WebApplicationContext org.springframework.web.context.support. WebApplicationContextUtils .getWebApplicationContext( ServletContext sc)
Find the root WebApplicationContext for this web application, which is typically loaded via org.springframework.web.context.ContextLoaderListener .
Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.
2. RequestContextUtils.getWebApplicationContext(request)メソッドコメント
WebApplicationContext org.springframework.web.servlet.support.
RequestContextUtils .getWebApplicationContext(
ServletRequest request) throws
IllegalStateException
Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing.