The Artima Developer Community
Sponsored Link

Java Buzz Forum
Curious about Spring transaction definitions?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
Curious about Spring transaction definitions? Posted: Apr 13, 2004 11:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: Curious about Spring transaction definitions?
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
The interface org.springframework.transaction.TransactionDefinitionis the place for you. (Although it's useful to keep a copy of a J2EE book around since Spring uses its declaration semantics.) It's got the different propogation and isolation types and a short but effective sentence or two on what they mean.

Tonight, two days before an initial install, I finally got around to wrapping my DAO methods with transactions. Most of them are just single-use actions anyway so it didn't matter quite as much, but once you start assembling them into larger services (like 'createPayment' or 'voidTransaction') then you start feeling some pain.

Like everything else in Spring declaring transactions is pretty easy. First let's say you have a bean:

    <bean name="ledgerActionDao"
            class="com.optiron.dao.LedgerActionHibernateDAO">
        <property name="sessionFactory"><ref bean="mySessionFactory" /></property>
    </bean>

Now you want to tell Spring to manipulate transactions around certain methods -- the default transaction support is to say any exception thrown will trigger a rollback. To do this you create a proxy and point it at your bean:

    <bean name="ledgerActionDaoProxy"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target"><ref bean="ledgerActionDao"/></property>
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
   </bean>

This is a little unrealistic (presumably we have some read-only methods that don't require a transaction). But it's still nifty... except for the fact that we have other objects already referring to 'ledgerActionDao'. I don't want to point them all at the proxy now because I'll surely miss something.

No problem. Just swap the names:

    <bean name="ledgerActionDaoTarget"
            class="com.optiron.dao.LedgerActionHibernateDAO">
...
    <bean name="ledgerActionDao"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target"><ref bean="ledgerActionDaoTarget"/></property>

And since the DAOs are all generated I took advantage of Spring's flexibility by declaring a set of patterns to match against all my DAOs instead of listing the methods for each one:

    <bean name="daoTxAttributes"
    class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
          <props>
            <prop key="find*">PROPAGATION_NOT_SUPPORTED</prop>
            <prop key="create*">PROPAGATION_REQUIRED</prop>
            <prop key="mark*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
    </bean>

And then you just reference it in the transaction declaration for each of your DAOs:

    <bean name="ledgerActionDao"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target"><ref bean="ledgerActionDaoTarget"/></property>
        <property name="transactionAttributeSource"><ref bean="daoTxAttributes" /></property>

This is good stuff... BTW, if you haven't seen it yet this presentation from Eduardo Issao Ito is a fantastic overview of Spring with plenty of examples throughout.

Read: Curious about Spring transaction definitions?

Topic: [Apr 2, 2004 11:55 PST] 12 Links Previous Topic   Next Topic Topic: Better, Faster, Lighter Java: Hibernate and Spring

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use