0 Replies Latest reply on Mar 7, 2011 6:21 PM by Dan Kane

    OptionalDataException when retrieving cached session

    Dan Kane Newbie

      JBoss AS 4.2.2, JBoss Cache 1.4.1sp5

       

      Thanks in advance for any guidance you can offer 

       

      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.

       

      What am I doing wrong?

       

      Code snippet:

       

            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; // Just process first 5 cache records for testing purposes

              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)

                    {

                      //... cannot deserialize session

                    }

                  }

                  else

                  {

                    //... has no session object

                  }

                }

              }

            }

            catch (Exception ex)

            {

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

            }