ejbCreate() ejbRemove()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    ejbCreate() ejbRemove()

    In my component class i implemeneted ejbCreate method which connects to the cloudscape database and returns connection. My question is Is it the right thing to do or should it be done in some other way?

    it's my first time with ejb.


    method ejbRemove() closes the database connection

    Code:
    public Connection ejbCreate() {
    	    Connection connection = null;
            try {
    		Class.forName("COM.cloudscape.core.RmiJdbcDriver");
    	    connection = DriverManager.getConnection("jdbc:cloudscape:rmi:books");
            } catch (ClassNotFoundException classNotFound) {
      	      System.out.println( "Driver Not Found");
      	      System.exit(1);
      	    } catch (SQLException sqlException) {
      	      System.out.println(sqlException.getMessage());
      		}
    	    return connection;		
    }
    
    public void ejbRemove(Connection connection) {
    try {
    	connection.close();
        } catch (SQLException e) {
    	e.printStackTrace();
        }

    thank You
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    No that's not the way the to do it. There must be a connection pool somewhere where you just request a connection when needed rather than having the connection code all over the place.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      something like ->

      Code:
      public JDCConnectionDriver(String driver, 
                      String url, 
                      String user, 
                      String password) 
              throws  ClassNotFoundException,
                      InstantiationException, 
                      IllegalAccessException, 
                      SQLException {
      
        DriverManager.registerDriver(this);
        Class.forName(driver).newInstance();
        pool = new JDCConnectionPool(url, user, password);
      }
      and then

      Code:
      public synchronized Connection getConnection() 
      	throws SQLException {
      
        JDCConnection c;
        for(int i = 0; i < connections.size(); i++) {
           c = (JDCConnection)connections.elementAt(i);
           if (c.lease()) {
              return c;
           }
        }
      
        Connection conn = DriverManager.getConnection(
      			url, user, password);
        c = new JDCConnection(conn, this);
        c.lease();
        connections.addElement(c);
        return c;
      }
      my ejbCreate function would be then the "public synchronized Connection getConnection() " function ?

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        i browsed the net i think i get it, will keep you posted

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          i changed it to

          Code:
          public void ejbCreate() {
          	    Connection connection = null;
          	    try{
          	        // get database connection
          	           connection = getConnection();
          	           Statement statement = connection.createStatement();    
          
          sql statements will be here    
          	    } catch (SQLException sqlException) {
          	  	      System.out.println(sqlException.getMessage());
            		}  		
          }
          
          private Connection getConnection(){
          	Connection temp = null;
          	 try {
          			Class.forName("COM.cloudscape.core.RmiJdbcDriver");
          			temp = DriverManager.getConnection("jdbc:cloudscape:rmi:books");
          			System.out.println("Connected to the database");
          	        } catch (ClassNotFoundException classNotFound) {
          	  	      System.out.println( "Driver Not Found");
          	  	      System.exit(1);
          	  	    } catch (SQLException sqlException) {
          	  	      System.out.println(sqlException.getMessage());
          	  		}
          		    return temp;
          	
          }
          ? now how does it look?

          Comment

          Working...