You can put the datasource in your data-sources.xml file, then put a reference
to it in your ejb-jar.xml deployment descriptor for your bean as follows:

         <resource-ref>
                <description>Datasource</description>
                <res-ref-name>jdbc/DSN</res-ref-name>
                <res-type>com.evermind.sql.OrionCMTDataSource</res-type>
                <res-auth>Container</res-auth>
         </resource-ref>

then in your bean you can write a method as below to get a connection from the
datasource through JNDI:

      private Connection getConnection()
                throws EJBException
        {
                // to do: modify datasource below to match your server
                final String DATASOURCE = "java:comp/env/jdbc/DSN";
                try
                {
                        InitialContext ic = new InitialContext();
                        DataSource ds = (DataSource)ic.lookup(DATASOURCE);
                        return ds.getConnection();
                }
                catch (Exception ex)
                {
                        throw new EJBException(ex.getMessage());
                }
        }

One improvement that you could make, that we eventually did before moving to
CMP, is to move the datasource string into an env-entry in your deployment
descriptor. That way, you can change datasources at deployment time instead of
having to recompile your bean.

Jeff Hubbach

Nimret Sandhu wrote:

> hello ~
>
> when i use bmp for persisting data to the database, it appears that i have
> to open a connection to the db and persist the bean's data. are there any
> connection pools or anything available within the app server that i can
> utilize for this? or is cmp the only way to take advantage of any built in
> connection pooling in the app server?
>
> i believe ejb 2.0 provides some support for this using DataSources/jndi but
> i am not sure. can someone else shed some more light on this topic?
>
> tia ~
> ---
> Nimret Sandhu
> http://nimret.dhs.org

--
Jeff Hubbach
Internet Developer
New Media Designs, Inc.
www.nmd.com




Reply via email to