JBoss Community

OptionalDataException when retrieving cached session

created by Dan Kane in JBoss Cache - View the full discussion

JBoss AS 4.2.2, JBoss Cache 1.4.1sp5

 

My appserver uses DB2 as a backend for JBoss Cache to persist sessions. I can kill the appserver, restart it and continue my session just fine - proving that it is storing and retrieving the session correctly.

 

However, I would like to write a standalone java app that accesses the session data in the JBOSSCACHE table, to inspect/verify it.

 

 

I can use a JDBCCacheLoader to connect to the DB. I create a Fqn and can use it to iterate through its children (the session records). I can even retrieve the byte[] that is supposedly the serialized form of the session object.

 

However, when I try to use an ObjectInputStream to readObject(), I always get an OptionalDataException, which makes no sense.

 

Code snippet:

 

 

OptionalDataException when retrieving cached session

  JDBCCacheLoader cl = new JDBCCacheLoader();

      Properties props = new Properties();

      props.setProperty("cache.jdbc.driver", "com.ibm.db2.jcc.DB2Driver");

      props.setProperty("cache.jdbc.url", "jdbc:db2:-//host.domain.com");

      props.setProperty("cache.jdbc.user", "username");

      props.setProperty("cache.jdbc.password", "password");

      cl.setConfig(props);

      try

      {

        cl.create();

        String[] path = new String[] { "JSESSION", "localhost", "ROOT" };

        Fqn root = new Fqn(path);

        Set children = cl.getChildrenNames(root);

        int count = 5;

        for (Object c : children)

        {

          if (count == 0)

          {

            break;

          }

          count--;

          if (c instanceof String)

          {

            String cStr = root.toString() "/" (String)c;

            Map m = cl.get(Fqn.fromString(cStr));

            Object obj = m.get(c);

            if (obj != null)

            {

              InputStream is = new ByteArrayInputStream((byte[])obj);

              try

              {

                ObjectInputStream ois = new ObjectInputStream(is);

                Object sess = ois.readObject(); // **OptionalDataException**

                sess.hashCode();

              }

              catch (Exception ex)

              {

                //...

              }

            }

            else

            {

              //...

            }

          }

        }

      }

      catch (Exception ex)

      {

        Logger.getLogger(MainFrame.class.getName()).log(Level.ERROR, null, ex);

      }

/code

 

Reply to this message by going to Community

Start a new discussion in JBoss Cache at Community