2 Replies Latest reply on Nov 17, 2003 4:40 PM by Jon Barnett

    connecting hsql thru stateless session bean

    yajbossuser Newbie

      hi
      plz tell me how to use a hsql database using a stateless session bean. plz give me link to some tutorial or sample code.

      i tried the foll code which works with jboss 2.2.2 but not with jboss 3.2.2:

      import java.rmi.RemoteException;
      import javax.rmi.PortableRemoteObject;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      import javax.ejb.*;
      import javax.rmi.*;
      import java.sql.*;
      import java.util.*;
      import java.io.*;
      import javax.naming.*;

      /**
      It includes empty method bodies for the methods
      prescribe by the SessionBean interface; these don't need to do
      anything in this simple example.
      */

      public class JDBC_ExampleBean implements SessionBean
      {
      Connection c = null;
      Statement st = null;
      Statement st2 = null;
      public void welcome()
      {
      String query="";
      String type="";
      try
      {
      getConnection();
      st = c.createStatement();
      st2 = c.createStatement();
      /* try
      {
      ResultSet res3 = st.executeQuery("select bearing_type_id from demands_table");
      while(res3.next())
      {
      type = res3.getString(1);
      System.out.println("type "+type);
      }
      }catch(Exception e){System.out.println(e.getMessage());}*/

      }
      catch(Exception e)
      {
      System.out.println("Exception occured");
      System.out.println(e.getMessage());
      }

      }

      /**
      Perform any initialization your bean needs
      */
      public void ejbCreate() {
      System.out.println("EJB is created");
      }
      /**
      Empty method body
      */
      public void ejbRemove() {
      System.out.println("EJB is removed");
      }
      /**
      Empty method body
      */
      public void ejbActivate() {}
      /**
      Empty method body
      */
      public void ejbPassivate() {}
      /**
      Empty method body
      */
      public void setSessionContext(SessionContext sc) {}
      public void getConnection()
      {
      try
      {
      Class.forName("org.hsql.jdbcDriver");
      c = DriverManager.getConnection("jdbc:HypersonicSQL:hsql://localhost:1476","sa","");
      // c = DriverManager.getConnection("jdbc:HypersonicSQL:hsql://10.109.13.7:1476","sa","");
      c.setAutoCommit(false);
      }
      catch(Exception e)
      {
      System.out.println(e.getMessage());
      }
      }
      }


      I get the following exception on server side:

      INFO [STDOUT] EJB is created
      INFO [STDOUT] No ClassLoaders found for: org.hsql.jdbcDriver
      INFO [STDOUT]Exception occured
      INFO [STDOUT]null

        • 1. Re: connecting hsql thru stateless session bean
          Jon Barnett Master

          You are trying to get a direct connection using JDBC. JBoss 3.2.2. does not provide for direct JDBC access.

          You should in any case be using the DataSource, java:/DefaultDS as the transactions are then managed within the context of EJB transactions.

          So one way would be to get the DataSource and get the connection, and close the statements and connection when you have finished with it.

          DataSource ds = (DataSource)(new InitialContext()).lookup("java:/DefaultDS");
          Connection connection = ds.getConnection();
          ...
          connection.close();


          Have a look at the free documents on the JBoss SourceForge site for more information.

          • 2. Re: connecting hsql thru stateless session bean
            Jon Barnett Master

            When I said JBoss 3.2.2 does not provide for direct JDBC connection, I meant with regards to the Hypersonic Database as the database is a created in-process and offers no service via a JDBC port. See JBOSS_HOME/server/default/deploy/hsqldb-ds.xml for more details about how the database is configured.