Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binding DataSource with JBoss

 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you show me the java coding you used to get the DB connection through datasource.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic