2

Consider a set of DOAs with methods similar to this

public void addObject(Long sessionId, Long clientId, Dom obj){...}

Now every domain pojo (Dom) has a sessionId property and for every insert, update or delete on a domain object a sessionId must be passed with setSessionId(Long sessionId)so we can know who does what. But it seems that this cuts across the all the data access stuff we and I think AOP would be a good tool to insert the sessionId in either a @Before(JoinPoint) or @Around(ProceedingJoinPoint) advise. Is this actually feasible? The DAOs are mostly Hibernate based with a few Spring StoredProcedure.

2
  • I'm confused... if the sessionId is part of the Dom object, why is it also a parameter in the DAo method?
    – skaffman
    Oct 5, 2009 at 21:42
  • Ooops my mistake but to clarify the sessionId gets set manually(programmatically) in the code, but the method above is a proposed solution where AOP can just insert the sessionId without the developer having to deal with it. The method signature is incorrect though as something will need to be passed into the method for sessionId. The advise would retrieve the sessionId from utility code and just insert it. Oct 5, 2009 at 21:50

3 Answers 3

1

Your sessionId parameter seem to belong to an audit aspect. In Hibernate, this auditing aspect is typically implemented in an implementation of org.hibernate.Interceptor.

You can implement many methods that correspond to life-cycle event for your entities and queries. In your implementation, it is easy to get access to your current sessionId (it could use a ThreadLocal variable). Clear and clean, fast ;-)

This is probably simpler than doing it yourself with AOP, especially as you get callbacks from Hibernate for life-cycle events.

1

An interceptor around your DAO layer will have trouble accounting for transitive persistence, i.e. hibernate operations (save, delete, ...) cascading to associated entities. A Hibernate session interceptor could.

In any case, if your interceptor can somehow discover the current session id, this is possible to do either way.

1

I don't see why you couldn't do this, I've actually done something similar (a little more complex) in the past, helped me not cross-refactor every class!

Have you thought of putting public void addObject(Long sessionId, Long clientId, Dom obj){...} in an father / upper level class? This method could delegate on each implementation..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.