In the Spring to configure Service

JTA in Spring

Development Environment: JDK1.5 (sun) + Myeclipse6.0 + Tomcat5.5 + ant 1.7.1 + MySql5.0.4

Framework version: JSF1.2 (sun) + hibernate3.3.1.GA + spring2.5.6

JTA (Java Transaction API)

Only supports the standard EJB Persistence (JTA) transaction management, and Spring can support the most popular persistence framework for transaction management.

In the Spring, through implementation org.springframework.transaction.PlatformTransactionManager interface to achieve a variety of persistence frameworks affairs.

Programs persistent

Spring to configure affairs that corresponds to category

JDBC

org.springframework.jdbc.datasource.DataSourceTransactionManager

JTA

org.springframework.transaction.jta.JtaTransactionManager

Hibernate

org.springframework.orm.hibernate3.HibernateTransactionManager

JPA

org.springframework.orm.jpa.JpaTransactionManager

Spring's transaction manager to provide only the implementation of existing Services API (Hibernate, JDBC, JTA) package, which itself does not provide specific services to achieve.

In the Spring of affairs org.springframework.transaction.TransactionDefinition interface definition. If you want a deeper understanding of the affairs Sping mechanism, it is necessary to understand the interface.

Spring in the spread of property would support a total of seven, can refer to Pok Man " Spring in the affairs of the dissemination of detailed property "And" doubts spring nested transaction "

If I want to achieve the following requirements: in a specific category, if the method name is insert, delete at the beginning of the method to support the safety of current affairs, if there is no current affairs, on the construction of a new services, other methods read-only.

Introduce the following requirements on the above two in the Spring configuration commonly used in the affairs of Ways:

Method 1: Using BeanNameAutoProxyCreator automatically create Service agent

<!--  -->
<bean  >
       <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Define transaction rules interceptor -->
<bean
   >
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
       <props>
           <prop key="insert*">PROPAGATION_REQUIRED</prop>
           <prop key="delete*">PROPAGATION_REQUIRED </prop>
           <prop key="*">readOnly</prop>
       </props>
    </property>
</bean>
<!-- Add to implement the transaction rules of BeanName -->
<bean
   >
    <property name="beanNames">
       <list>
           <value>persondao</value>
       </list>
    </property>
    <property name="interceptorNames">
       <list>
           <value>transactionInterceptor</value>
       </list>
    </property>
</bean>

<bean     >
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean
      >
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

Method II: Using AOP to manage the affairs Principle

With the large quantity Sping project code, you will find more and more to many configurations, and cumbersome. In Sping2.0 after, Sping introduced a simplified XML configuration. Concrete can look white Jiangnan " Simplify the Spring - configuration file . "

In use before the Second, add packages required packages as follows: aspectjrt.jar, aspectjweaver.jar

ApplicationContext.xml and the head are defined as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

Then configure Affairs

<bean  >
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- This configuration has thought of AOP, looking for this surface is com .sms.freeborders.dao.hbimplAll of the classes under -->

<aop:config>
    <aop:pointcut
       expression="execution(* com.sms.freeborders.dao.hbimpl.*.*(..))" />
    <aop:advisor advice-ref="txAdvice"
       pointcut-ref="allManagerMethod" />
</aop:config>

<!-- Configure transaction rules -->

<tx:advice>
    <tx:attributes>
       <tx:method name="insert *" />
       <tx:method name="delete*" />
       <tx:method name="*" read-only="true" />
    </tx:attributes>
</tx:advice>

<bean  >
       <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean
      >
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
分类:Java 时间:2009-02-27 人气:879
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.543 (s). 12 q(s)