File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes BEA/Weblogic and the fly likes DataSource in ejb-jar.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "DataSource in ejb-jar.xml" Watch "DataSource in ejb-jar.xml" New topic

DataSource in ejb-jar.xml

peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
I reposting my question :
Chris Thanks for all your help .
Could you please suggest me where to specify this datasource in ejb-jar.xml & jndi name with datasource in ejb-weblogic-jar.xml
Following is my ejb-jar.xml:
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' ''>
<home> myapp.MYAPPWebServiceHome</home>
<remote> myapp.MYAPPWebService</remote>
<ejb-class> myapp.MYAPPWebServiceBean</ejb-class>
following is my ejb-weblogic-jar.xml :
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN' ''>

Following is my code in the ejb method:
Hashtable ht = new Hashtable();
// Get a context for the JNDI look up
ctx = new InitialContext(ht);
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup ("mydataSource-OracleThinDriverPool");
conn = ds.getConnection();
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
You don't NEED to specify the datasource in your ejb descriptors. It is just a way to avoid hardcoding the datasource's JNDI name in your code. If all of your EJBs use the same datasource and there is no immediate need to use separate datasource that I would put the JNDI name directly in code. Create a helper class for retrieving the connection and have it coded there in one place. I have created a class to this in my projects.
Here is a simple class for this purpose. It uses another class to get an initial context went is not shown:

Usage looks like this:

The thing that I like is that I now have strong compile-time checking on my connection pool usage. If you use the DD and you make a typo in 1 of your 10 ejbs, then you won't know until the first time you access that bean. This way you have one central place to change all your connection pool configuration and if it works for one, it will work for all.
The other benefit is that if you want to know where a connection pool is being used, comment out one line and let the compiler find all the dangling references.
Of course, this means that a change in configuration means a change in code... but I can live with that. If it turns out to be a bad idea in the future than I will just refactor it out!

Imports, comments, and additional code has been left out to protect the innoccent.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Of course this doesn't work with CMP. In that case you have to specify the datasource in your deployment descriptor.
peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
Thank you very much.
If you don't mind could you please advise
what is the diffrence between Txdatasource and datasource.

I have diffrent databases on diffrent machines. All i need to do is to pass the url,sid etc
for each of the machine/database and get the data.
for ex>
one is
url :jdbc racle:thin:@mydevelopmentmc:1521 EMO
user: abc
my another url is
url :jdbc racle:thin:@mytestmc:1521:TEST
user: abctest
I am planning to create two connection pools one for each
also two corresponding data sources.
Is there any better solution.please advise
Thanks again
I agree. Here's the link:
subject: DataSource in ejb-jar.xml
It's not a secret anymore!