This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

singleton issue with AOP implementation

Aug 17th, 2006, 04:33 AM

Okay, Following is the requirement.

We have JSF layer, Controller layer, Service Layer and DAO layer in our application. We want, whenever we insert some data into database, it will get inserted along with the login user's id, so that it can be tracked who has inserted it.

We are using AOP feature at the DAO layer for this, so that any "save" method coming through the DAO layer gets pointcut at this stage, and the user id gets inserted into the object before going to the Database.

We are taking the user id from the session at the controller level, and putting it into a singleton class (IdentityManager). Because Session is not available at the DAO layer, we get the user id here from that singleton class.

So far so good. It is working perfectly for single login. Now the problem is happenning with multiple login. It is always taking the last login user id while inserting.

Say, in one session I login with user_1, another session I login with user_2. Now user_2 has logged in last, so if I insert anything from user_1 login, IdentityManager class passes id as "user_2" to be inserted with. Because it is a singleton class, it is maintaining single instance only and gets updated with the latest value only.

We tried even with using ThreadLocal, but ThreadLocal does not solve the issue because Spring serves a single thread model, ThreadLocal cannot show its magic in Spring framework.