0
votes

Looking at the below two classes and AOP configuration, I'm uncertain about whether this is right at all? I've configured a pointcut on the PartnerService, but only use the session in PartnerDao. Will this safely begin a new session (and transaction) which I can use in PartnerDao?

These are my classes

PartnerService.java:

public class PartnerService {

    private PartnerDao dao;

    public void setDao(PartnerDao dao) {
        this.dao = dao;
    }    

    PartnerDao getDao() {
        return dao;
    }

    public List<Partner> getPartners() {
        return getDao().getPartners();
    }

    public void createPartner(Partner partner) {
        getDao().createPartner(partner);
    }

}

PartnerDao.java

    public class PartnerDao {

    private HibernateTemplate template;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.template = new HibernateTemplate(sessionFactory);
    }    

    HibernateTemplate getTemplate() {
        return template;
    }

    public List<Partner> getPartners() {
        return getTemplate().execute(new HibernateCallback<List<Partner>>() {

            @Override
            public List<Partner> doInHibernate(Session s) throws HibernateException {
                Criteria c = s.createCriteria(Partner.class);
                return c.list();
            }                    
        });        
    }

    public void createPartner(Partner partner) {
        getTemplate().save(partner);
    }

}

Now, I'd like to use AOP to manage transactions. I've got the following AOP configuration in applicationContext.xml:

<aop:config>        
    <aop:pointcut id="serviceMethods" expression="within(com.company.pas.service.*)"/>
    <aop:advisor advice-ref="companyTransactionAdvise" pointcut-ref="serviceMethods"/>
</aop:config>  

If you look at my AOP configuration, I'm configuring a pointcut for the PartnerService (which is located in com.company.pas.service). However, I'm only dealing with the session in PartnerDao. The way I'm instantiating these classes is that I've got a ServiceFactory which returns a PartnerService with an autowired instance of PartnerDao.

1

1 Answers

0
votes

Why not using Spring AOP Transactional Management? Using annotations it will be as easy as annotating your Service methods with @Transactional.