Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of org.springframework.transaction.interceptor.TransactionProxyFactoryBean in Spring

Tags:

java

spring

In my project all the manager classes implemented like this pattern,

<bean id="companyManagerTxProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="proxyTargetClass"><value>true</value></property>
        <property name="transactionAttributes">
             <props>
                <prop key="create*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean id="companyAdminManager" parent="companyManagerTxProxy" scope="prototype">
        <property name="target">
            <bean class="lucky.src.bto.controllerImpl.CompanyAdminManagerImpl">
            </bean>
        </property>
    </bean>

can you please explain me why we are using org.springframework.transaction.interceptor.TransactionProxyFactoryBean in creating all the manager's beans. what is the exactly use of it?

like image 601
Lokesh Rathor Avatar asked Apr 11 '16 07:04

Lokesh Rathor


People also ask

Does spring provide Programaatic transaction management?

Spring provides two means of programmatic transaction management: Using the TransactionTemplate. Using a PlatformTransactionManager implementation directly.

What does @transactional do what is the PlatformTransactionManager?

TransactionTemplate provides a set of callback-based APIs to manage transactions manually. In order to use it, we should first initialize it with a PlatformTransactionManager. The PlatformTransactionManager helps the template to create, commit or roll back transactions.


1 Answers

This is a broad question because there are conceptual explanation to be done here. In very short: TransactionProxyFactoryBean is part of Spring's Transaction support and allows applications to have a "transactional" behavior in server-independent way. In other words: You may replace EJB Container Managed beans if you configure your services using TransactionProxyFactoryBean.

Please refer to Spring Documentation

You may need to understand the concept of Transaction overall. But a very short explanation is:

Transactions (read or write of information) must be ACID and ensure that we need to give "transactional" behavior the software resources (code) which interact with the database (there may be other transactions).

The above explanation touches a bit on AOP (Aspect Oriented Programming) where in, the behavior you want to give with the transactions is "aspect".

There are usually three things that need to be configured:

transactionManager, target and transactionAttribute.

The transactionAttribute is where you give transactional behavior for the read and writes for your resources. In your example, your companyAdminManager is given trnasactional behavior. The companyAdminManager in turn must be configured with datasource which would have db url, user/pass and other pertinent information.

Here are two good explanations (explains each and every line)

1.) Click this to to read each line of configuration explained

2.) I find the following blog post by Scot to be basic and easy to understand example with explanation. Please read click here

like image 142
Nirmal Avatar answered Oct 11 '22 05:10

Nirmal