16

I have the following session scoped bean:

@ManagedBean
@Component
@Scope(proxyMode= ScopedProxyMode.TARGET_CLASS, value="session")
public class SessionData implements Serializable {}

and I store tomcat sessions in a database. The problem is that, when the application tries to deserialize a stored session, I receive the following error:

 org.apache.catalina.session.PersistentManagerBase.swapIn Error deserializing Session EE913D2ACAD49EB55EDA657A54DFA2CB: {1}
 java.lang.ClassNotFoundException: de.myproject.SessionData$$EnhancerBySpringCGLIB$$768b59b9

It seems that it serialized actually the whole Spring context, and obviously there is no such class de.myproject.SessionData$$EnhancerBySpringCGLIB$$768b59b9 after server restarts, so I receive the aforementioned exception.

Is there a way to avoid this, so that the session-scoped bean is serialized properly?

UPDATE: There is an issue regards this which marked as resolved without comments, however I still face it.

joragupra
  • 692
  • 1
  • 12
  • 23
vtor
  • 8,989
  • 7
  • 51
  • 67

3 Answers3

1

Please have a try:

using: import org.springframework.test.util.AopTestUtils;

 Serializable readyToSerialize = AopTestUtils.getUltimateTargetObject(yourInstance);

before serialize it.

Note: this code is usefull to understund the problem, if this work, you have to analyze the project architecture and dependecies, to better accomplish for production code. First of all, why you need to serialize a ScopedProxyMode.TARGET_CLASS

Guaido79
  • 1,261
  • 11
  • 18
0

Having a bean with a scope session doesn't mean that the bean is serializable and that it can be stored in a session.

As you can guess from the name of the class, a proxy class is generated at runtime with a different name at each startup. This explains why a problem occurs at deserialization.

I guess you try to add this SessionData as an attribute of the web session. You should not. Store your POJO data in the web session without using beans.

If you use the bean to inject database connections or similar objects, forget it. You can just use session scope beans for particular contexts which don't feet your requirements I guess.

mvera
  • 904
  • 12
  • 23
  • 2
    This is JSF bean, and JSF serializes automatically all Session scoped beans and stores them in session. – vtor May 23 '16 at 15:13
0

i don't know well the your task, but in my opinion a data object like this should not be a spring bean because spring bean should be business logic bean, controller bean and so on and not session dto.

for this reason i consider thag you should try to think why you want store the data of your spring bean, and try to decopled the data that you want in http session, @sessionattribute of springmvc,from the business logic bean that shoud nor be session aware.

i hooe that this can help you to change your implementation stategy in order to find the solution of your problem

Valerio Vaudi
  • 4,199
  • 2
  • 24
  • 23