Results 1 to 2 of 2

Thread: Declarative transactions using Tiger annotations

  1. #1
    Join Date
    Aug 2004
    Location
    St. Petersburg, Russia
    Posts
    25

    Default Declarative transactions using Tiger annotations

    Hello All!

    :idea:
    I just want to share the code, that allow using JDK 5 annotations for declarative transactions.

    Transacted.java:
    Code:
    package spring.transaction;
    
    import java.lang.annotation.*;
    
    import org.springframework.transaction.TransactionDefinition;
    
    /**
     * @author Sergey Astakhov
     * @see spring.transaction.JDK5TransactionAttributeSource
     */
    @Inherited
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.METHOD})
    public @interface Transacted
    {
      int propagation() default TransactionDefinition.PROPAGATION_REQUIRED;
      int isolation() default TransactionDefinition.ISOLATION_DEFAULT;
      boolean readOnly() default false;
    }
    JDK5TransactionAttributeSource.java
    Code:
    package spring.transaction;
    
    import java.lang.reflect.Method;
    
    import org.springframework.transaction.interceptor.TransactionAttributeSource;
    import org.springframework.transaction.interceptor.TransactionAttribute;
    import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
    
    /**
     * @author Sergey Astakhov
     */
    public class JDK5TransactionAttributeSource implements TransactionAttributeSource
    {
      public TransactionAttribute getTransactionAttribute(Method method, Class targetClass)
      {
        Transacted desc = method.getAnnotation(Transacted.class);
        if( desc==null )
          {
            if( targetClass==null ) targetClass = method.getDeclaringClass();
    
            desc = (Transacted) targetClass.getAnnotation(Transacted.class);
    
            if( desc==null ) return null;
          }
    
        DefaultTransactionAttribute attr = new DefaultTransactionAttribute();
    
        attr.setPropagationBehavior( desc.propagation() );
        attr.setIsolationLevel( desc.isolation() );
        attr.setReadOnly( desc.readOnly() );
    
        return attr;
      }
    }
    declarativeTransactions.xml
    Code:
    <?xml version="1.0" encoding="Windows-1251"?>
    
    <!DOCTYPE beans
     PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
      <bean id="autoproxy"
            class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
    
      <bean id="transactionAttributeSource"
            class="spring.transaction.JDK5TransactionAttributeSource"
            />
    
      <bean id="transactionInterceptor"
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributeSource"><ref bean="transactionAttributeSource"/></property>
      </bean>
    
      <bean id="transactionAdvisor"
            class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
        <constructor-arg><ref bean="transactionInterceptor"/></constructor-arg>
      </bean>
    </beans>
    Usage:

    Code:
    @Transacted
    public class ServiceImpl implements Service
    &#123;
      @Transacted&#40; readOnly=true &#41;
      public Set<EntityInfo> getEntityList&#40;&#41;
      &#123;
       ...
      &#125;
    
      public void deleteEntity&#40;EntityInfo info&#41;
      &#123;
       ...
      &#125;
    &#125;
    We are using this code in our project - works very nice.

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Thanks. I finished some code myself in late Sept, but just haven't had a chance to document it and publish it as a package like we want to.

    It's here:
    http://cvs.sourceforge.net/viewcvs.p...samples/tiger/

    Your code looks somewhat similar but doesn't do anything with rollback rules, which the code I did does handle.

    Good to see somebody is using annotations!
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. transactions, annotations and session facade
    By dvoytenko in forum Architecture
    Replies: 0
    Last Post: Oct 4th, 2005, 11:45 AM
  2. Replies: 4
    Last Post: Apr 19th, 2005, 08:24 AM
  3. Replies: 2
    Last Post: Apr 11th, 2005, 08:53 AM
  4. Replies: 4
    Last Post: Jan 25th, 2005, 02:46 PM
  5. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •