12 Replies Latest reply on Aug 22, 2008 11:41 AM by Peter Johnson

    Using Connection Pool in EJB 3.0

    Bimal Thapa Newbie

      Hi all,
      I am in the requirement of developing EJB without entity beans and simply using stateless beans. In this scenario how can i connect to database? and Can i use the existing connection pool. It's saved in XML file as a configuration file. This is working connection pool in web container. How to make it run in ejb jar file. It has been successfully tested on web modules. How to change it so that it will run on ejb jar.

      Thanks in advance,
      Mabimal

        • 1. Re: Using Connection Pool in EJB 3.0
          jaikiran pai Master

           

          "mabimal" wrote:
          Hi all,
          I am in the requirement of developing EJB without entity beans and simply using stateless beans. In this scenario how can i connect to database?


          Through datasource.

          "mabimal" wrote:

          and Can i use the existing connection pool.


          Yes, you can.

          "mabimal" wrote:

          It's saved in XML file as a configuration file. This is working connection pool in web container.


          Which xml file? JBoss has its own datasource pooling implementation where you configure the datasource through a -ds.xml file. Is this the same that you are talking about? If not, please provide more details.

          "mabimal" wrote:

          How to make it run in ejb jar file. It has been successfully tested on web modules. How to change it so that it will run on ejb jar.


          The datasource is created at the server level. It will be accessible through JNDI for both the web component and the ejb component.


          • 2. Re: Using Connection Pool in EJB 3.0
            Bimal Thapa Newbie

            Thank you so much jaikiran ji,
            I am delighted to get the reply. Now that you have answered most of the questions, i want to provide details on XML file, i m currently not using -ds.xml file of jboss rather using custom file named configurator.xml in which the root user,password,and IP where the database server is located.How can i make ejb read this file so that my connection to the database will be created?

            Thank you in advance.

            • 3. Re: Using Connection Pool in EJB 3.0
              jaikiran pai Master

               

              "mabimal" wrote:
              How can i make ejb read this file


              I don't know how you are reading this file currently in your web application. But you can use:

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




              • 4. Re: Using Connection Pool in EJB 3.0
                Peter Johnson Master

                 

                mabimal wrote:
                How can i make ejb read this file


                Are you asking how to configure EJB3 such that it uses your configuration file to connect to the database? If so, as far as I know, EJB3 looks up a datasource in JNDI. Therefore, all you have to do is create a service that reads your configuration file and creates a JNDI name for your datasource.

                • 5. Re: Using Connection Pool in EJB 3.0
                  Bimal Thapa Newbie

                  PeterJ Wrote:

                  Are you asking how to configure EJB3 such that it uses your configuration file to connect to the database?

                  Yes i am asking exactly the same question. So please provide me the samples how to do this.

                  Thank you So Much

                  • 6. Re: Using Connection Pool in EJB 3.0
                    Bimal Thapa Newbie

                     

                    "PeterJ" wrote:


                    Are you asking how to configure EJB3 such that it uses your configuration file to connect to the database?


                    Yes i am asking exactly the same question. So please provide some samples with code please.

                    Thank you PeterJ
                    Mabimal

                    • 7. Re: Using Connection Pool in EJB 3.0
                      Peter Johnson Master

                      The only thing I can think of is to look at the code in JBossAS that reads the *-ds.xml file to configure a datasource, and use that code as the basis for your code to read your configuration file to configure a data source.

                      Of course, you could instead create a *-ds.xml file based on your configuration file and then use that.

                      My guess is that the first alternative (first paragraph above) would take several months of effort, and the second alternative (second paragraph above) would take several minutes.

                      • 8. Re: Using Connection Pool in EJB 3.0
                        Bimal Thapa Newbie

                        Thank you peterJ,

                        I will try to move on with the second paragraph.

                        I think in this case we must rely on the server.

                        And is there any alternative that without depending upon the server to do this. Means can we build our EJB's datasource such that we will not depend upon server (In the case of server switch we will have to rewrite the whole datasource file).

                        If we can then please provide some examples.
                        Thank you
                        Mabimal



                        • 9. Re: Using Connection Pool in EJB 3.0
                          Peter Johnson Master

                          As far as I know, datasource configuration is an app server dependent thing - each one does it in its own way.

                          • 10. Re: Using Connection Pool in EJB 3.0
                            Bimal Thapa Newbie

                            Thank you PeterJ

                            I will try to continue with that.

                            Thank you
                            Mabimal

                            • 11. Re: Using Connection Pool in EJB 3.0
                              Bimal Thapa Newbie

                              Thank you PeterJ,

                              I am now moving with the datasource. But confusion again is that can (I am using Oracle database and data must be accessed from 3 users) we write 3 users and password in a single datasource file or we should make 3 different datasource file. And if we make 3 different datasources files what must be the name of the -ds.xml file. And How to access this -ds.xml file from the Code..

                              Thank you in advance anticipating the answer
                              mabimal

                              • 12. Re: Using Connection Pool in EJB 3.0
                                Peter Johnson Master

                                Use 3 *-ds.xml file, each with its own user/password but all referencing the same database in the url.

                                Differentiate among them by using different JNDI names is each *-ds.xml file. Your code would use the JNDI name that corresponds to the desired datasource.