rotty3000/liferay-portal — pull request #233 by laszlocsontos (2013-06-12 11:23:19+00:00)
Title: [61GA3-REGRESSION-CLEANUP] LPS-34596 Sharding doesn't work in the current builds

Hi Ray,

Do you remember my former (https://github.com/rotty3000/liferay-portal/pull/226) pull request?

You were absolutely right about that, I've been debugging this issue for several days since then and the problem's been much more deeper than I originally thought.

Sharding needs ShardPersistenceAdvice around beans in the persistence layer. If PACL is off, DoPrivilegedFactory doesn't do anything, so things are going smoothly.

However we have a very special corner case: there are beans which form a circular reference chain (eg. UserPersImpl -> GroupPersImpl -> OrgPersImpl -> UserPersImpl ) and this way they mutually depend on each other in terms of instantiation. In this situation when PACL is on, DoPrivilegedFactory wraps these beans to early before AOP advices could have been applied to them.

Because of this, BeanReferenceAnnotationBeanPostProcessor injects references to these beans into service beans with a wrong wrapping. The correct wrapping should be PACL(AOP(*PersistenceImpl)), whereas DoPrivilegedFactory does its job too early and PACL(*PersistenceImpl) gets injected instead.

I fixed it in the following way:

* In DoPrivilegedFactory "under-construction" beans (early bean references) are isolated and set aside for later use, no immediate wrapping is done in this stage.
* When the whole Spring context is created, BeanReferenceRefreshUtil is used to revisit these beans, wrap with PACL's invocation handler and reinject them.

Thanks for reviewing!

Cheers,
Laszlo.