Announcement Announcement Module
Collapse
No announcement yet.
@Transactional DAO and proxy-target-class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional DAO and proxy-target-class

    Hi!

    I have to say, I've been reading all the documentation and some forum posts, but I can't get things to work the way I'd like.

    I'm new to Spring and I'm developing an application using jsf+spring+hibernate3. I was having all sorts of problems with "lazy inizialization" and "two open sessions". So I decided to use the Open Session in view filter. The I started getting some other error (now I can't recall), so that's when I read something about using annotations, the @Transactional.

    It seemed a good idea so I added that to my DAO classes, but the thing is that I only get that to work if I add the proxy-target-class="true" in the config file, which I understand is not recommended, for what I can read on the Spring documentation. I would really appreciate if someone could point me out what I'm doing wrong, or tell me if that's not really a big problem.

    My DAO structure is the following, I have a class called Notas (for example):
    Code:
    @Transactional(readOnly = true)
    public class NotasDAO extends GenericDAO implements NotasInterfaceDAO
    This only contains some finds and the update method, annotated as readonly false
    So this Generic DAO is as follows:
    Code:
    @Transactional(readOnly = true)
    public class GenericDAO extends HibernateDaoSupport implements GenericInterfaceDAO{
    
         @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
         public void insert(final AbstractEntity object) {
             getHibernateTemplate().save(object);
         }
    
         @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
         public void delete(final AbstractEntity object) {
             getHibernateTemplate().delete(object);
         }
    }
    Then the NotasInterface only contains
    Code:
    public interface NotasInterfaceDAO extends GenericInterfaceDAO{
        public List<Notas> findAll();
        public Notas findById(final int id);
    
    }
    And the Generic Interface
    Code:
    public interface GenericInterfaceDAO {
         public void insert(final AbstractEntity object);
         public void delete(final AbstractEntity object);
         public List find(String query);
         public List find(String query, int max);
    }
    And my applicationContext contains:
    Code:
     <bean id="transactionManager"
             class="org.springframework.orm.hibernate3.HibernateTransactionManager">
             <property name="sessionFactory" ref="sessionFactory" />
         </bean>
    
          <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
         
          <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="dataSource" ref="dataSource"/>
            <property name="sessionFactory" ref="sessionFactory" />
          </bean>
    
           <bean id="NotasDAO"
             class="datos.NotasDAO">
             <property name="sessionFactory" ref="sessionFactory" />
         </bean>
    So basically I added the @Transactional to methods that need to write on the DB. At first so I could try this, I just added the @Transactional to the update methods, but it required proxy-target-class set to true also.

    I've read the documentation (all the documents you're gonna recommend) but I still can't figure out how to get this right. Maybe it's my DAO structure, or maybe I'm doing something else wrong. I'd really appreciate if someone could give me some hints on how to get this right.
    Tanks in advance.

  • #2
    Originally posted by 1000i1 View Post
    ...
    So basically I added the @Transactional to methods that need to write on the DB. At first so I could try this, I just added the @Transactional to the update methods, but it required proxy-target-class set to true also.

    I've read the documentation (all the documents you're gonna recommend) but I still can't figure out how to get this right. Maybe it's my DAO structure, or maybe I'm doing something else wrong. I'd really appreciate if someone could give me some hints on how to get this right.
    Tanks in advance.
    Spring is able to use two proxying mechanisms - jdk- and cglib-based. Feel free to read more about that here. So, if you use jdk proxies, the methods that not present at the interface are not adviced.

    Also the statement 'proxy-target-class="true" in the config file ... is not recommended' is incorrect. Both approaches have pros and cons and spring team just recommends to use jdk proxies if possible. If it's not possible or you want to get slight performance boost there is absolutely no objection to use cglib proxies.

    Comment


    • #3
      I know the two methods work, what I don't know is why I'm being forced to use CGLIB (due to the errors I get if I don't). I'd like to know what I'm doing that prevents me of working with the JDK proxies if I remove the proxy-target-class setting.

      Comment


      • #4
        Originally posted by 1000i1 View Post
        I know the two methods work, what I don't know is why I'm being forced to use CGLIB (due to the errors I get if I don't). I'd like to know what I'm doing that prevents me of working with the JDK proxies if I remove the proxy-target-class setting.
        I already told about that - if you use jdk proxies, the methods that not present at the interface are not adviced. I.e. jdk-proxies are aware only about interface methods, so, tx aspect logic may be applied only at interface method call.

        Comment


        • #5
          Ok, sorry for reading your answer wrong. I'll try it.

          Thanks!

          Comment

          Working...
          X