Java JNDI Connection Pool

Discussions

General J2EE: Java JNDI Connection Pool

  1. Java JNDI Connection Pool (2 messages)

    Hi,

    I'm writing a Simple Java Application. Which will generate a Connection Pool.




    public static void main (String args []) throws SQLException, NamingException
    {
    Context ctx = null;
    try
    {
    Hashtable env = new Hashtable (5);
    env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put (Context.PROVIDER_URL, "file:/tmp/JNDI");
    ctx = new InitialContext(env);
    }
    catch (NamingException ne)
    {
    ne.printStackTrace();
    }
     
    OracleDataSource ods = null;
     
    ods = new OracleDataSource();
    ods.setUser("userName");
    ods.setPassword("password");
    ods.setURL("jdbc:oracle:thin:@DBSERVER:1521:ORACLE_WORLD");
     
    ctx.bind("jdbc/sampledb", ods);
     
    ods = (OracleDataSource)ctx.lookup("jdbc/sampledb");

    Connection conn = ods.getConnection();
    Statement stmt = conn.createStatement ();
    ResultSet rset = stmt.executeQuery ("select USER from dual");
     
    while (rset.next ())
    {
    System.out.println ("User name is " + rset.getString (1));
    }
     
    rset.close();
    rset = null;
     
    stmt.close();
    stmt = null;
     
    conn.close();
    conn = null;
    }





    It's Working Fine. Now I want this Application to be independent of any 3rd Party ApI's like OracleDataSource, and to implement the ConnectionPoolDataSource.

    So, I replaced this section:


    ods = (OracleDataSource)ctx.lookup("jdbc/sampledb");
    Connection conn = ods.getConnection();



    of above Code with:


    javax.sql.ConnectionPoolDataSource cpds = (javax.sql.ConnectionPoolDataSource) context.lookup("jdbc/sampledb");
    javax.sql.PooledConnection pc = cpds.getPooledConnection();
    Connection conn = pc.getConnection();




    it throws Exception. So if can anyone help me!!!.

    Threaded Messages (2)

  2. Java JNDI Connection Pool[ Go to top ]

    Hi Manas,

       You bound the "OracleDataSource" object to JNDI.And once you got the same object through lookup, you are trying to typecast it to an incompatible object.

    Giri.
  3. Password Encryption[ Go to top ]

    Hi For this exact example, I want the password to be saved in an encrypted format. What is the shortest path to have the password encrypted? Thanks