wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes Binding DataSource with JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Binding DataSource with JBoss" Watch "Binding DataSource with JBoss" New topic
Author

Binding DataSource with JBoss

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi,

I'm working on an exercise where in I need to access a database from a servlet. Previously I used DriverManager class to load the necessary drivers before Connection can be established. But this time, I wanted to use DataSource instead of DriverManager.

I have few queries which I'm eager to know
1) Can we lookup for the bound DataSource with JBoss from a standalone client. I tried to google on this, but saw very different opinions from different people. Some saying "Yes" and some "No".
2) If we want to deploy our war in JBoss, what are the descriptor files needed as a mandatory where in we can mention our DataSource and its JNDI name. I know that we mention these details in web.xml. But apart from this file, which other is needed to bind the DataSource. Also would we be putting the other xml file in WEB-INF or META-INF. Apart from web.xml, I copied jboss-web.xml from internet, modified it and copied it to WEB-INF, before I can jar into a war file. Is this a correct procedure?
3) Where else do we mention about the datasource and its binding apart from web.xml, when we want to deploy in Tomcat server. Do we need to change server.xml or any other xml.


All my questions are very basic and unfortunately could not get any solid information on this in internet as its more confusing for me. If any one could answer my questions , that would be a great help.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Originally posted by Schandha Ravi:
1) Can we lookup for the bound DataSource with JBoss from a standalone client. I tried to google on this, but saw very different opinions from different people. Some saying "Yes" and some "No".


Yes, you "can" lookup a datasource from a standalone client. You have to configure the datasource with use-java-context as false in the -ds.xml file. More details here. However, its "not recommended" to lookup the datasource from a remote client for reasons mentioned in that wiki.

Originally posted by Schandha Ravi:
2) If we want to deploy our war in JBoss, what are the descriptor files needed as a mandatory where in we can mention our DataSource and its JNDI name. I know that we mention these details in web.xml. But apart from this file, which other is needed to bind the DataSource. Also would we be putting the other xml file in WEB-INF or META-INF. Apart from web.xml, I copied jboss-web.xml from internet, modified it and copied it to WEB-INF, before I can jar into a war file. Is this a correct procedure?
3) Where else do we mention about the datasource and its binding apart from web.xml, when we want to deploy in Tomcat server. Do we need to change server.xml or any other xml.


Let's keep the web.xml and other deployment descriptors aside, for a while, till you get the datasource working. In JBoss, to deploy a datasource, all you need is a *-ds.xml file which has to be placed in the deploy folder. See these for more details:

DSdotXML

ConfigDataSources

JBoss by default has some sample datasources in its %JBOSS_HOME%\docs\examples\jca folder, which you can use as a reference.

The -ds.xml file is all that you need for a datasource deployment. The reason why people mention modifying the web.xml (and jboss-web.xml) to configure the datasource is to provide an additional indirection to your application, so that if the jndi-name of the datasource changes tomorrow, then your lookup code need not worry about the change. This is more of a best practice.
[ March 08, 2008: Message edited by: Jaikiran Pai ]

[My Blog] [JavaRanch Journal]
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi JaiKiran,

As mentioned I copied mysql-ds.xml to deploy folder of JBoss and set my JNDI as MySQL as mentioned below


and also changed web.xml as


I have removed previously created jboss-web.xml file from WEB-INF folder.
But while deploying my application I got the following error message



From the bold text, I see that resource-ref element of jboss-web.xml is not set. Does this mean that jboss-web.xml should be present along with web.xml and mysql-ds.xml.

Also in case I want to deploy my war file in Tomcat instead of Jboss, do we need to change server.xml file.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

If you edited the web.xml to add the resource-ref for the datasource then you also have to place a jboss-web.xml containing a resource-ref element for the datasource. Something like this:

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Originally posted by Schandha Ravi:
Also in case I want to deploy my war file in Tomcat instead of Jboss, do we need to change server.xml file.


Sorry, i dont know have much knowledge of how the datasources are deployed in Tomcat. You might want to ask this in our Tomcat forum
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Thanks JaiKiran,

I removed resource-ref from web.xml and did not include any jboss-web.xml file. This worked out for me.

I guess, this is the best way of deploying DataSources as, this ensures portability. By replacing mysql-ds.xml with some other xml I can use different databases.
Shane Connors
Greenhorn

Joined: Oct 29, 2009
Posts: 9
Hi,

Can you show me the java coding you used to get the DB connection through datasource.
 
 
subject: Binding DataSource with JBoss