Reduce the transaction configuration of the Spring Architecture)

Source: Internet
Author: User
Tags abstract definition
Note: The original article was published on it168

Spring revolutionizes the previous programming model and introduces new concepts such as IOC, which is widely used. Currently, most j2ee projects use the Spring framework. The biggest problem with Spring is that there are too many configuration files, so that you not only need to maintain the program code, but also need to maintain the relevant configuration files. The most typical is the transaction configuration (Note: Here "transaction configuration" refers to "declarative transaction configuration"). In Spring, the transaction configuration not only defines the bean of the object, you also need to define a bean for transaction proxy. if you have n classes that need to introduce transactions, you must define 2n beans. The cost of maintaining these beans is very expensive, so the transaction configuration must be reduced. If you are using Spring-based architecture design, as a good architecture designer, you should simplify some public aspects so that project developers only care about the business logic of the project, instead of spending too much energy on things beyond the business logic. Therefore, as a good architecture, we should simplify transaction management so that programmers can spend less on programming.

 

 

Technology Transaction Manager Built-in transaction support
JDBC DataSurceTransactionManagerJtaTransactionManager All classes in the JdbcTemplate and org. springframework. jdbc. object packages
IBATIS DataSourceTransactionManagerJtaTransactionManager SqlMapClientTemplate and SqlClientTemplate
Hibernate HibernateTransactionManagerJtaTransactionManager HibernateTemplate and HibernateInterceptor
JDO JdoTransactionManagerJtaTransactionManager JdoTemplate and JdoInterceptor
ApacheOJB PersistenceBrokerTransactionManagerJtaTransactionManager PersistenceBrokerTemplate
JMS JmsTransactionManager JmsTemplate

 

 

 

 

 

When dividing a transaction, we need to define the transaction, that is, to configure the transaction attributes. The attributes of a transaction include the propagation industry, isolation level, timeout value, and read-only mark. The TransactionAttribute interface specifies which exceptions will cause a rollback and which ones should be submitted at one time.

 

 

 

 

 

(1) Use ProxyFactoryBean and TransactionInterceptor

 

<! -- Define the local data source -->

 

<Bean id = "dataSource" name = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource" destroy-method = "close">
<Property name = "driverClassName" value = "$ {jdbc. driverClassName}"/>
<Property name = "url" value = "$ {jdbc. url}"/>
<Property name = "username" value = "$ {jdbc. username}"/>
<Property name = "password" value = "$ {jdbc. password}"/>
</Bean>

 

<! --! Define the Transaction Manager for a single jdbc data source -->
<Bean id = "transactionManager" class = "org. springframework. jdbc. datasource. cetcetransactionmanager">
<Property name = "dataSource" ref = "dataSource"/>
</Bean>

 

<! -Define the interceptor -->
<Bean id = "transactionInterceptor"
Class = "org. springframework. transaction. interceptor. TransactionInterceptor">
<Property name = "transactionManager">
<Ref bean = "transactionManager"/>
</Property>
<Property name = "transactionAttributes">
<Props>
<Prop key = "insert *"> PROPAGATION_REQUIRED </prop>
<Prop key = "update *"> PROPAGATION_REQUIRED </prop>
<Prop key = "save *"> PROPAGATION_REQUIRED </prop>
<Prop key = "find *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "get *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "*"> PROPAGATION_SUPPORTS, readOnly </prop>
</Props>
</Property>
</Bean>

 

<! -Define business objects -->
<Bean id = "com.prs.application.ehld.sample.biz.service.sampleService.tar get"
Class = "com. prs. application. ehld. sample. biz. service. impl. SampleServiceImpl">
<Property name = "userInfoDAO"
Ref = "com. prs. application. ehld. sample. integration. dao. userInfoDAO">
</Property>
</Bean>

 

<! -Define the transaction proxy object of the Business Object -->
<Bean id = "com. prs. application. ehld. sample. biz. service. sampleService" class = "org. springframeword. aop. framework. ProxyFacgtoryBean">
<Property name = "target"
Ref = "com.prs.application.ehld.sample.biz.service.sampleService.tar get">
</Property>
<Property name = "interceptorNames">
<Value> transactionInterceptor </value>
</Property>
</Bean>

 

You can use ProxyFacgtoryBean and TransactionInterceptor to control transactions. All objects requiring transaction control can share the transaction attribute of transactionInterceptor.

 

(2) Use TransactionProxyFactoryBean
 
<! -Define business objects -->
<Bean id = "com.prs.application.ehld.sample.biz.service.sampleService.tar get"
Class = "com. prs. application. ehld. sample. biz. service. impl. SampleServiceImpl">
<Property name = "userInfoDAO"
Ref = "com. prs. application. ehld. sample. integration. dao. userInfoDAO">
</Property>
</Bean>

 

<! -Define the transaction proxy object of the Business Object -->
<Bean id = "com. prs. application. ehld. sample. biz. service. sampleService" class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean"
Abstract = "true">
<Property name = "transactionManager">
<Ref bean = "transactionManager"/>
</Property>
<Property name = "target"
Ref = "com.prs.application.ehld.sample.biz.service.sampleService.tar get"/>
<Property name = "transactionAttributes">
<Props>
<Prop key = "insert *"> PROPAGATION_REQUIRED </prop>
<Prop key = "update *"> PROPAGATION_REQUIRED </prop>
<Prop key = "save *"> PROPAGATION_REQUIRED </prop>
<Prop key = "find *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "get *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "*"> PROPAGATION_SUPPORTS, readOnly </prop>
</Props>
</Property>
</Bean>

 

To use TransactionProxyFactoryBean, You need to define your own transaction attributes for each proxy object.

 

(3) Simplify configuration by using TransactionProxyFactoryBean and abstract attributes
This is also the most commonly used declarative transaction configuration method.

 

 
<! -- Transaction control proxy abstract definition -->
<Bean id = "baseTransactionProxy" class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean"
Abstract = "true">
<Property name = "transactionManager">
<Ref bean = "transactionManager"/>
</Property>
<Property name = "transactionAttributes">
<Props>
<Prop key = "insert *"> PROPAGATION_REQUIRED </prop>
<Prop key = "update *"> PROPAGATION_REQUIRED </prop>
<Prop key = "save *"> PROPAGATION_REQUIRED </prop>
<Prop key = "find *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "get *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "*"> PROPAGATION_SUPPORTS, readOnly </prop>
</Props>
</Property>
</Bean>

 

<! -Define business objects -->
<Bean id = "com.prs.application.ehld.sample.biz.service.sampleService.tar get"
Class = "com. prs. application. ehld. sample. biz. service. impl. SampleServiceImpl">
<Property name = "userInfoDAO"
Ref = "com. prs. application. ehld. sample. integration. dao. userInfoDAO">
</Property>
</Bean>

 

<! -Define the transaction proxy object of the Business Object -->
<Bean id = "com. prs. application. ehld. sample. biz. service. sampleService" parent = "baseTransactionProxy">
<Property name = "target"
Ref = "com.prs.application.ehld.sample.biz.service.sampleService.tar get">
</Property>
</Bean>

 

The abstract attribute allows the proxy object to share a defined transaction attribute, simplifying the configuration.

 

(4) use BeanNameAutoProxyCreator
<! -Define the interceptor -->
<Bean id = "transactionInterceptor"
Class = "org. springframework. transaction. interceptor. TransactionInterceptor">
<Property name = "transactionManager">
<Ref bean = "transactionManager"/>
</Property>
<Property name = "transactionAttributes">
<Props>
<Prop key = "insert *"> PROPAGATION_REQUIRED </prop>
<Prop key = "update *"> PROPAGATION_REQUIRED </prop>
<Prop key = "save *"> PROPAGATION_REQUIRED </prop>
<Prop key = "find *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "get *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "*"> PROPAGATION_SUPPORTS, readOnly </prop>
</Props>
</Property>
</Bean>

 

<! -Define the bean alias automatic proxy Builder -->
<Bean id = "autoProxyCreator"
Class = "org. springframework. aop. framework. autoproxy. BeanNameAutoProxyCreator">
<Property name = "interceptorNames">
<Value> transactionInterceptor </value>
</Property>
<Property name = "beanNames">
<List>
<Idref local = "com. prs. application. ehld. sample. biz. service. sampleService"/>
</List>
</Property>
</Bean>

 

<! -Define business objects -->
<Bean id = "com. prs. application. ehld. sample. biz. service. sampleService"
Class = "com. prs. application. ehld. sample. biz. service. impl. SampleServiceImpl">
<Property name = "userInfoDAO"
Ref = "com. prs. application. ehld. sample. integration. dao. userInfoDAO">
</Property>
</Bean>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Spring declarative transaction configuration methodsTo control transactions in Spring, you must first select the appropriate Transaction Manager, and then select the transaction policy for the program. If there is only one transactional resource, you can select one from the "single resource" PlatformTransactionManger implementation, which includes: cetcetransactionmanager, HibernateTransactionManager, JdoTransactionManager, PersistenceBrokerTransactionManager, and JmsTransactionManager. Choose based on your database persistence technology. If your project runs on a server that supports JTA, JtaTransactionManger will be selected and multi-resource transactions will be supported. The following table provides reference for selecting the appropriate Transaction Manager.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.