Discussions

EJB design: JDBC calls Within CMP

  1. JDBC calls Within CMP (6 messages)

    hello,

    i need a JDBC call CMP to lookup to some Data.
    my coding looks like:


    protected java.math.BigDecimal getNextKey()
    throws RemoteException, FinderException, javax.naming.NamingException {

    java.math.BigDecimal nextKey = new java.math.BigDecimal(0);
    PreparedStatement pstmt = null;
    Connection con = null;

    try {
    //Properties env;
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
    env.put(Context.PROVIDER_URL, "IIOP://localhost:900/");

    InitialContext ic = new InitialContext(env);

    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@195.82.85.101:1521:dc02","uap","uap");

    pstmt = con.prepareStatement("select uap_logs_seq.nextval as key from dual");
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    String id = rs.getString("key");
    nextKey = new java.math.BigDecimal(id);
    pstmt.close();
    con.close();

    return nextKey;
    }
    catch (Exception e) {
    throw new FinderException(e.toString());
    }

    finally {
    //Release DB Connection
    try {
    pstmt.close();
    con.close();
    }
    catch (Exception e) { }
    }


    }

    The problem is that the database_url ist hardcoded, i don't know how to implement it flexible, e.g. if the database changes. My problem is that i don't know how to get the currently used databaseconnection from the EJBServer.
    Does anyone give me a hint.

    Threaded Messages (6)

  2. JDBC calls Within CMP[ Go to top ]

    Which app server do you use? If it's not completely outdated, you should be able to use a JNDI lookup to get access to a DataSource object. You can ask that for a connection from the connection pool you've set up for your CMP beans.

    Something like

      ctx = new InitialContext();
      javax.sql.DataSource ds = (javax.sql.DataSource)
            ctx.lookup ("myDataSource");
      java.sql.Connection conn = ds.getConnection();


    Regards,
    Stefan
  3. JDBC calls Within CMP[ Go to top ]

    above is a perfect solution for J2EE access to database connection.

    or else u cud even write a helper class which acts a factory for database connection.
  4. JDBC calls Within CMP[ Go to top ]

    That's right!!! Hard coding the connection in the Bean doesn't preserve its portability.As mentioned above, you can
    use the getConnection method. Here is a sample and it works fine.
              private Connection getConnection()
        throws SQLException
      {
        InitialContext initCtx = null;
        try {
          initCtx = new InitialContext();
          DataSource ds = (javax.sql.DataSource)
            initCtx.lookup("java:comp/env/jdbc/demoPool");
          return ds.getConnection();
        } catch(NamingException ne) {
          
          throw new EJBException(ne);
        } finally {
          try {
            if(initCtx != null) initCtx.close();
          } catch(NamingException ne) {
            
            throw new EJBException(ne);
          }
        }
      }
    After adding this code , you have mention your database URL and driver in the deployment descriptor and in the weblogic.properties file as well.
                       Cheers,
                        Ragu.
  5. JDBC calls Within CMP[ Go to top ]

    I'am using WebSphere Applikation Server 3.5
  6. JDBC calls Within CMP[ Go to top ]

    thanks for replay.

    i've implemented connection pooling in VAJ WTE v3.5
    However, I still have a problem. When I start WTE Persistent Name Server
    and
    check DataSource Configuration, I see that my datasource named
    'SUN01dc02' to
    Oracle database exists there. But, when I execute this code:
    i've got a problem when i'd like to the current DBconnection


    private java.sql.Connection getConnection()
        throws SQLException,EJBException {

        java.sql.Connection conn=null;
        InitialContext initCtx = null;

     try {
        initCtx = new InitialContext();
       javax.sql.DataSource ds = (javax.sql.DataSource)
    initCtx.lookup("jdbc/SUN01dc02");
       conn = ds.getConnection();
       return conn;
     }

     catch(NamingException ne) {
        throw new EJBException(ne.toString());

     }
     catch (SQLException se){
        throw new EJBException(se.toString());
     }
     catch (Exception e){
        throw new EJBException(e.toString());
     }

     finally {
       try {
      if(initCtx != null) initCtx.close();
       }
       catch(NamingException ne) {
      throw new EJBException(ne);
       }
     }//finally
    }

    the Datasource looks fine but when i trie to getConnection
    an SQLException throw in line conn = ds.getConnection();
    java.sql.SQLException: Ung├╝ltige Argumente in Aufruf
    I trie to translate it to english 'Invalid argument in call'

    Christine


  7. When you declared your datasource in websphere, did you provide the username/password? My guess is no, thanks to the sample code you provided in your first post. So when connecting the data source you must provide username/password: getConnection( userName, password ) or include these piece of information within the data source specification : jdbc:oracle:thin:uap/uap@195.82.85.101:1521:dc02

    I hope this will sort your problem

    Jacques