10 Replies Latest reply on Sep 1, 2008 5:03 AM by mabimal

    Accessing JBoss Datasource from EJB 3.0

      Hi all,

      I have been searching for accessing JBoss Datasource from EJB 3.0, but i could only get the result for accessing datasource from client. I am in need to develop EJB 3.0 such that it accesses the JBoss Datasource and connect to the database and ready to answer the queries from clients.

      How can i achieve this also i need to make EJB ready by connecting to Database when i deploy the ejb-jar file in the JBoss server.

      In the anticipation of solution
      Mabimal

        • 1. Re: Accessing JBoss Datasource from EJB 3.0
          jaikiran

           

          "mabimal" wrote:

          I am in need to develop EJB 3.0 such that it accesses the JBoss Datasource and connect to the database and ready to answer the queries from clients.



          When you say EJB3, do you mean, EJB3 entities? Or is it EJB3 Session Beans?

          DataSource(s) are bound to the JNDI tree when deployed. So if your datasource contains something like:
          <?xml version="1.0" encoding="UTF-8"?>
          
          
          <datasources>
           <local-tx-datasource>
           <jndi-name>MyAppDS</jndi-name>
          
           <connection-url>...</connection-url>
           <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
           <user-name>xyz</user-name>
           <password>abc</password>
          
           </local-tx-datasource>
          
           </datasources>


          Then in your EJB3 bean, you can lookup the datasource as follows:

          Context ctx = new InitialContext();
          DataSource ds = (DataSource) ctx.lookup("java:/MyAppDS");
          






          • 2. Re: Accessing JBoss Datasource from EJB 3.0

             

            "jaikiran" wrote:


            When you say EJB3, do you mean, EJB3 entities? Or is it EJB3 Session Beans?



            It is EJB3 Session Beans. I am not using entity beans due to my business requirement. I am beginner to EJB and i am having the concept that EJB will be executed only after the call from clients. Can we run our database centric code prior to the client call. If we can plz provide some examples jaikiranji.

            In the anticipation of the solution
            Mabimal

            • 3. Re: Accessing JBoss Datasource from EJB 3.0
              jaikiran

               

              I am beginner to EJB and i am having the concept that EJB will be executed only after the call from clients.


              That's correct.

              Can we run our database centric code prior to the client call. If we can plz provide some examples


              You can have a look at th @Service provided by JBoss http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/jboss_extensions.html. @Service provides lifecycle management APIs (which are explained in that document) through which you can perform the startup activities related to database.

              • 4. Re: Accessing JBoss Datasource from EJB 3.0

                Thank you Jaikiranji,

                I am trying to move on with it.

                Regards,
                Mabimal

                • 5. Re: Accessing JBoss Datasource from EJB 3.0

                  Hello all,

                  I am building just the ejb-module(EJB-JAR) not the Enterprise Archive (EAR) file. So please help me to find the contextRoot of the ejb-module. What is the code to achieve the contextRoot of the EJB-module.

                  In the anticipation of the solution
                  Mabimal

                  • 6. Re: Accessing JBoss Datasource from EJB 3.0
                    jaikiran

                     

                    "mabimal" wrote:
                    Hello all,

                    I am building just the ejb-module(EJB-JAR) not the Enterprise Archive (EAR) file. So please help me to find the contextRoot of the ejb-module. What is the code to achieve the contextRoot of the EJB-module.


                    You have to tell us, what exactly you are trying to do with the context root? Why do require that?

                    • 7. Re: Accessing JBoss Datasource from EJB 3.0

                      Hi jaikiranji,
                      I actually want to access my Configuration.xml text by keeping it in the context root, but where to keep this file ( I am in confusion of it too). Here in this xml file, i want to keep username,password,ip,maxcon,idletime etc. which i want to access from class file and pass it to ConnectionPool class. And whenever my client accesses EJB, i will call this class and maintain the Connection. But how to achieve this?

                      Thank you
                      Mabimal

                      • 8. Re: Accessing JBoss Datasource from EJB 3.0
                        jaikiran

                        Maybe this is what you want:

                        MyAppEjb.jar
                        |
                        |
                        |----- configuration.xml
                        |
                        |
                        |----- org
                        | |
                        | |--- myapp
                        | | |
                        | | |--- MyConfigurationReader.class
                        



                        In your MyConfigurationReader.class (which reads the configuration.xml) you could do this:


                        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("configuration.xml");
                        

                        Then pass that inputStream to an xml parser.


                        • 9. Re: Accessing JBoss Datasource from EJB 3.0

                          Thank you jaikiranji

                          Mabimal

                          • 10. Re: Accessing JBoss Datasource from EJB 3.0

                            Hello all,

                            I am now not using datasource but using my configuration file by reading it and sending to the XMLParser class.

                            We need to copy the jar of EJB to client application in order to communicate to EJB by the client. I am now having confusion that, we have lot's of classes supporting connection pool, and there is just an EJB class that responses the client request and helps to establish connection to database.

                            Currently in my application the connection pool code and EJB interface are in a single jar which i think will cause the problem of more than one instance of the connection pool be created and to avoid that we need to make the connection pool jar and the EJB interface jar different.

                            But EJB interface and class must be linked with connection pool in order to have connection. So how to achieve this?

                            In the anticipation of the solution
                            Mabimal