Search code examples
datasourcejndinotserializableexceptioninitial-context

NotSerializableException with BasicDataSource in JNDI


I am setting up a standalone JNDI and loading a Datasource to the JNDI. DataSource I use is: org.apache.commons.dbcp.BasicDataSource

The JNDI is set up as follows

        String detectorHost = InetAddress.getLocalHost().getHostName();
        System.out.println("detectorHost: " + detectorHost);
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        final NamingBeanImpl namingInfo = new NamingBeanImpl();
        namingInfo.start();

        final Main JNDIServer = new Main();

        JNDIServer.setNamingInfo( namingInfo );
        JNDIServer.setPort( 5400 );
        JNDIServer.setBindAddress(InetAddress.getLocalHost().getHostName());
        JNDIServer.start();

        final Hashtable _properties = new Hashtable();
        _properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        _properties.put(Context.PROVIDER_URL,            "jnp://" + InetAddress.getLocalHost().getHostName() + ":5400");

        final Context _context = new InitialContext(_properties);
        _context.createSubcontext("jdbc");
        String JNDI_PATH = "jdbc" + "/" + "mydbname";
        _context.bind(JNDI_PATH, getDataSource());

I get the following exception

javax.naming.CommunicationException [Root exception is     java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource]
    at org.jnp.interfaces.NamingContext.bind(NamingContext.java:677)
    at org.jnp.interfaces.NamingContext.bind(NamingContext.java:611)
    at javax.naming.InitialContext.bind(Unknown Source)
    at com.lombardrisk.reform.integration.ReformIntegration.createJNDIServer(ReformIntegration.java:93)
    at     com.lombardrisk.reform.integration.ReformIntegration.main(ReformIntegration.java:44)
Caused by: java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.rmi.MarshalledObject.<init>(Unknown Source)
    at org.jnp.interfaces.MarshalledValuePair.<init>(MarshalledValuePair.java:65)
    at org.jnp.interfaces.NamingContext.createMarshalledValuePair(NamingContext.java:1425)
    at org.jnp.interfaces.NamingContext.bind(NamingContext.java:640)

I don't quite follow why I am getting a NotSerializableException exception, this is a local JNDI in the same JVM and not a remote JNDI. Not sure why this occurs.

Can some one advise what is wrong here.

regards D


Solution

  • The exception was misleading. The exception became clear after adding the following to the JVM startup

    -Dsun.io.serialization.extendedDebugInfo=true

    It appears I was not using the correct libraries. I have explained the full problem and solution here in this link. https://community.jboss.org/thread/241498

    //Code to start a JNDI Server and a test client for the JNDI Context
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    import org.apache.log4j.Logger;
    import org.jnp.server.Main;
    import org.jnp.server.NamingBeanImpl;
    
    //Snippet of the code
    
        System.setProperty("java.rmi.server.hostname", "localhost");
        System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        System.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        final NamingBeanImpl namingInfo = new NamingBeanImpl();            
        namingInfo.start();
    
        final Main jndiServer = new Main();
    
    
        jndiServer.setNamingInfo(namingInfo);
        jndiServer.setPort(1099);
        jndiServer.setBindAddress("localhost");
        jndiServer.setRmiPort(1098);
        jndiServer.setRmiBindAddress("localhost");
        jndiServer.start();   
        final Hashtable<String, String> _properties = new Hashtable<String, String> ();
        _properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        _properties.put(Context.PROVIDER_URL,            "jnp://" + "localhost" + ":1099");
        final Context _context = new InitialContext(_properties);
        _context.createSubcontext("jdbc");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
        System.out.println("jdbcTemplate:  " + jdbcTemplate.getClass());
        System.out.println("getDataSource(): " + getDataSource().getClass());
        _context.bind("/jdbc/reformDS", getDataSource());
    
    //JNDI started
    
    
    //Test the JNDI context
    // There are 2 ways to test - 
    // Option 1: create a HashTable and pass the properties to the InitialContext constructor
        final Hashtable<String, String> _properties = new Hashtable<String, String> ();
        _properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        _properties.put(Context.PROVIDER_URL,            "jnp://" + "localhost" + ":1099");
    
    final Context _context = new InitialContext(_properties);
    Object obj = _context.lookup("/jdbc/reformDS");
    if (null != obj) {
    System.out.println("OBJ: " + obj.getClass());
               org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)obj;
               JdbcTemplate jdbcTemplate2 = new JdbcTemplate(ds);
               String sql = String.format("update MESSAGE_LOG set PROCESS_INSTANCE_ID = 123456 where ID =42395 ");      
               int update = jdbcTemplate2.update(sql);
               System.out.println("Update*****************: " + update);
               }
    
    
    // Option 2: Set the System.properties and call the InitialContext 
    System.getProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.getProperty(Context.PROVIDER_URL,            "jnp://" + "localhost" + ":1099");
    final Context _context = new InitialContext();
    Object obj = _context.lookup("/jdbc/reformDS");
    if (null != obj) {
    System.out.println("OBJ: " + obj.getClass());
               org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)obj;
               JdbcTemplate jdbcTemplate2 = new JdbcTemplate(ds);
               String sql = String.format("update MESSAGE_LOG set PROCESS_INSTANCE_ID = 123456 where ID =42395 ");      
               int update = jdbcTemplate2.update(sql);
               System.out.println("Update*****************: " + update);
               }