1

I need a functionality where the user can cancel the running query if she prefers, So I thought that I would serialize the hibernate session and then deserialize it back to cancel that specific query.

So I did something like:

  1. Serialize and set it in the DB.
  2. Get it back, and then call session.cancelQuery().

Below is the code I use:

ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject();
Session ss = (Session) o;

But this gives me an exception at readObject():

java.io.InvalidObjectException: could not resolve session factory during session deserialization [uuid=95d6a048-677b-42f0-9b9f-7e62fd68b533, name=null]

So I wanted to know, if it is even possible to serialize and deserialize a hibernate session object.

Edit: Well I also found this, but not sure if this is correct or not!

Community
  • 1
  • 1
rd22
  • 1,032
  • 1
  • 18
  • 34
  • I really don't understand what you are trying to achieve. Do you really want to store the database session inside the database? – vojta Dec 07 '15 at 06:45
  • @vojta yes, this I want to store the serialized session object in the DB and then deserialize it back to session object. – rd22 Dec 07 '15 at 06:47

1 Answers1

3

It is not possible A hibernate session is used to get a physical connection to the database, which means network layer is involved. It uses a socket, a network stack, the underlying hardware - and all of them are system specific and not managed by JVM.

Serialization is only good if you are dealing with the data.

aksappy
  • 3,400
  • 3
  • 23
  • 49
  • Would you be so kind to provide a source for this, if there is one :) ?, so that I can read more about it. – rd22 Dec 07 '15 at 07:01
  • 1
    A source for what? Sockets by design is an OS level interpretation than a JVM property. Moreover, resources cannot be serialized and data can be. That is what Serialization is supposed for. A simple google search will help you . See these in the mean time. http://stackoverflow.com/questions/16851070/java-what-can-and-what-cant-be-serialized http://www.jusfortechies.com/java/core-java/serialization.php – aksappy Dec 07 '15 at 07:06