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>
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of In the Spring to configure Service

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate to use the principle of

    The use of hibernate, implementation of data persistence. Has the following several processes. One configuration database connection information. Hibernate.config 2 configuration mapping. 3 use: the use of the process are the following steps: 3.1: Ge ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries