How to ensure proper binding of a data source
alexm Sep 29, 2004 1:30 PMHi,
I am using JBoss 3.2.5 on Fedora Linux. I've followed steps in the latest O'Reilly book Enterprise JavaBeans and in JBoss manual for configuring data sources, but have received varying results. For example, by following the steps in the O'Reilly book my JBoss bound the data source to the default HyperSonic (?) database. I've figured so much that the problem is in how the data source is named and referenced, but I am still puzzled as to what the right way is to name and reference a JDBC data source.
I've identified four places of interest (given on the example of MySQL DB):
1. mysql-ds.xml
2. jbosscmp-jdbc.xml
3. ejb-jar.xml
4. lookup code in Java
mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>SomeDB</jndi-name> <connection-url>jdbc:mysql://host:port/dbname</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>bla</user-name> <password>bla</password> </local-tx-datasource> </datasources>
jbosscmp-jdbc.xml
<?xml version="1.0" encoding="UTF-8"?> <jbosscmp-jdbc> <defaults> <datasource>java:/SomeDB</datasource> <datasource-mapping>mySQL</datasource-mapping> </defaults> </jbosscmp-jdbc>
In ejb-jar.xml
... <resource-ref> <res-ref-name>jdbc/SomeDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> ...
In the Java code:
... Context ictx = new InitialContext(); DataSource someDS = (DataSource) ictx.lookup("jdbc/SomeDB"); ...
These files were placed in the right locations.
The above combination doesn't work well. Using the JNDI name jdbc/SomeDB in mysql-ds.xml and java:/jdbc/SomeDB jbosscmp-jdbc.xml gives the same lookup problems. Instead of trying out all the possible combinations in the universe, can't someone simply explain what the right way to do this is and why that is so?
For example, how does JBoss bind the resource reference name such as jdbc/SomeDB? Based on the name of this field, jdbc/SomeDB is just the name of the reference, but not necessarily the name of the resource, so how does the binding occur? I've read that jdbc prefix should be used for JDBC data sources, but where should it be used, in some of the locations above or all of them? And what about the prefixes java:, java:/comp/env/jdbc, etc. I've found conflicting information on the net.
I should mention that I did get the whole thing to work but in the way different than explained in the book. The trick was to use java:/someDB in the Java lookup. What I didn't understand is what the role of the resource reference field was when I was referencing the resource directly based on its JNDI name and not based on the resource reference name?
I admit that I may have some holes in my understanding of this topic, hence this post :).
Thanks,
Alex