Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JBoss 3.2 and MySQL - Could not dereference DataSource object

 
Tom Marrs
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following exception:
javax.naming.NamingException: Could not dereference object [Root exception is java.lang.NullPointerException]
when I try to connect to a DataSource with the following code (this is from a Servlet):

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/JackalopeDB");
conn = ds.getConnection();
-------------------------------------------
JBoss deploys the MBean for the database properly.
Environment:
JBoss Version: jboss-3.2.0_tomcat-4.1.24
Database Server: MySQL v. 4.0.12
Database Driver: mysql-connector-java-3.1.0-alpha
Setup:
I don't use jboss.xml or jboss-web.xml
Here's the relevant portion of web.xml:
<resource-ref>
<description>Jackalope DB Connection</description>
<res-ref-name>jdbc/JackalopeDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
----------------------------------------------------
I copied the Driver's Jar file to %JBOSS_DIST%/server/default/lib to make sure it was on the JBoss CLASSPATH.
I copied %JBOSS_DIST%/docs/examples/jca/mysql-ds.xml to %JBOSS_DIST%/server/default/deploy and modified it to look like:
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/JackalopeDB</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jackalopedb?autoReconnect=true</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>jackalope</user-name>
<password>jackalope</password>
</local-tx-datasource>
</datasources>

I also had the same NullPointerException when I used MySQL Server v. 3.23.52 and MySQL Driver Version 2.0.14.
I don't think the problem is with the version of MySQL.
I must've configured things incorrectly.
What am I doing wrong?
Did I copy the driver to the right directory?
Does mysql-ds.xml look OK?
Is there another XML file that I need to modify. If so, which one and how?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mysql-ds.xml ?? i'll have to check this file, coz I've never used it.
when I set up a datasource in jboss (using mysql as well) I modify mysql-service.xml, and put it in the deploy directory in jboss. If I have more than 1 datasource, I can name it any way I want, as long as it finishes with the "service" word:
mysql1-service.xml
mysql2-service.xml
as you can read, this is not a full answer to your questions.. i'm still wondering about mysql-ds.xml ...
please correct me if i'm wrong...
cheers
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.. i just found out that in the 3.2 release you can use either mysql-service.xml or the new mysql-ds.xml.. :roll:
have you checked this link?
http://www.mail-archive.com/jboss-user@lists.sourceforge.net/msg26911.html
it might help ya!
 
Tom Marrs
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andres,
mysql-ds.xml is the new way to do this under JBoss 3.2
I couldn't find mysql-service.xml in the docs/examples/jca directory because they've removed it from JBoss's CVS.
I found 2 reasonable-looking versions of mysql-service.xml, but neither of them work anymore under 3.2.
I get the following exception:
2003-04-25 10:39:36,725 INFO [org.jboss.system.ServiceConfigurator] Problem configuring service jboss.jca:service=LocalTxDS,name=MySqlDS
org.jboss.deployment.DeploymentException: No Attribute found with name: JndiName
...
2003-04-25 10:39:37,156 DEBUG [org.jboss.system.ServiceController] recording that jboss.jca:service=LocalTxCM,name=MySqlDS depends on jboss.jca:service=LocalTxDS,name=MySqlDS
2003-04-25 10:39:37,156 DEBUG [org.jboss.system.ServiceConfigurator] considering ManagedConnectionFactoryName with object name jboss.jca:service=LocalTxDS,name=MySqlDS
2003-04-25 10:39:37,156 INFO [org.jboss.system.ServiceConfigurator] Problem configuring service jboss.jca:service=LocalTxCM,name=MySqlDS
org.jboss.deployment.DeploymentException: Exception setting attribute javax.management.Attribute: name=ManagedConnectionFactoryName value=jboss.jca:service=LocalTxDS,name=MySqlDS on mbean jboss.jca:service=LocalTxCM,name=MySqlDS; - nested throwable: (javax.management.AttributeNotFoundException: Writable attribute 'ManagedConnectionFactoryName' not found)
...
Caused by: javax.management.AttributeNotFoundException: Writable attribute 'ManagedConnectionFactoryName' not found
at org.jboss.mx.capability.ReflectedMBeanDispatcher.setAttribute(ReflectedMBeanDispatcher.java:190)
at org.jboss.mx.server.MBeanServerImpl.setAttribute(MBeanServerImpl.java:503)
at org.jboss.system.ServiceConfigurator.setAttribute(ServiceConfigurator.java:468)
... 60 more
2003-04-25 10:39:37,616 DEBUG [org.jboss.system.ServiceController] Creating service jboss.jca:service=LocalTxCM,name=MySqlDS
2003-04-25 10:39:37,616 DEBUG [org.jboss.system.ServiceController] Ignoring create request for service: jboss.jca:service=LocalTxCM,name=MySqlDS
2003-04-25 10:39:37,616 DEBUG [org.jboss.system.ServiceController] Creating service jboss.jca:service=LocalTxDS,name=MySqlDS
2003-04-25 10:39:37,616 DEBUG [org.jboss.system.ServiceController] Ignoring create request for service: jboss.jca:service=LocalTxDS,name=MySqlDS
2003-04-25 10:39:37,626 DEBUG [org.jboss.management.j2ee.LocalJBossServerDomain] handleNotification: javax.management.Notification[source=jboss.system:service=ServiceDeployer,type=org.jboss.deployment.SubDeployer.create,sequenceNumber=51,timeStamp=1051288777616,message=null,userData=org.jboss.deployment.DeploymentInfo@49e92f0e { url=file:/C:/jboss-3.2.0_tomcat-4.1.24/server/default/deploy/mysql-service.xml }
deployer: org.jboss.deployment.SARDeployer@78a212
Do you currently use JBoss 3.2? If you're able to configure and use MySQL, could you please send me your copy of mysql-service.xml? My email is available in my user profile. Thanks.
Tom
 
Darryl A. J. Staflund
Ranch Hand
Posts: 314
2
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Do you think maybe JBoss is returning a null because jackalopes don't exist? ;-)
Darryl
 
Tom Marrs
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cute.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. tricky one... :roll:
in mysql-service.xml i have something like this..
<attribute name="JndiName">MySqlDS</attribute>
if you check MySqlDS that's the actual name, and you are referring to jackalope...please recheck that... i've also sent you mysql-service.xml...
good luck and let us know..
 
bill lubx
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like your server is looking for jndi MySqlDS but you have configured as “jdbc/JackalopeDB”, in mysql-ds.xml,
I am not sure if "MySqlDS" is the only valid jndi name for DBSource in JBoss, but certainly server knows this is a MYsql DBSource and looking for it by the name "MySqlDS"..
Try this:
1.change “jdbc/JackalopeDB” back to “MySqlDS” in mysql-ds.xml,
2.seems you are using mysql connector driver, In this case, this would better be changed: <driver-class>org.gjt.mm.mysql.Driver</driver-class> to: <driver-class>com.mysql.jdbc.Driver</driver-class>,
3.drop the mysql-ds.xml in %JOSS_DIST% /server/default/deploy
4.restart the jboss at http://localhost:8080/jmx-console/, under jboss.jca, make sure this line is there: name=MySqlDS,service=LocalTxCM , or you could check with server log to see if this message is there: 2003-06-21 21:04:17,578 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.MySqlDS] Binding object 'org.jboss.resource.adapter.jdbc.WrapperDataSource@1a76eff' into JNDI at 'java:/MySqlDS',
5.change look up in your code to use “MySqlDS”
good luck.
bill
 
JAVA OS
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According my experience , your error is just because you "lookup" datasource jndi in the client bean,
you should invoke "lookup" datasource method in the Bean deployed in the server ,all seem to be OK;
just like (a method in serverBean ,not in client):
public String getData(){
String result = null;
Context context = null;
Connection con = null;
Statement stmt = null;
try {
context = new InitialContext();
Object ref = context.lookup("java:comp/env/jdbc/OracleDS");
javax.sql.DataSource ds = (DataSource) ref;
con = ds.getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from hr.jobs");
if (rs.next()) {
result = rs.getString("JOB_TITLE");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (stmt != null)
stmt.close();
if (con != null)
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
 
Satish Murthy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing exactly the same problem. Did this get resolved.
 
Fred Sv
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran in same problem. It's a tricky one but easy to fix !
Problem is the JNDI name: original code was probably running with Tomcat, but with JBoss, name is slightly different.
In place of :
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/JackalopeDB");
use :
DataSource ds = (DataSource) ctx.lookup("java:/jdbc/JackalopeDB");
To be sure about JNDI name, check logs when you start jboss and look for a line like this :
11:44:44,294 INFO [jdbc/JackalopeDB] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=LocalTxCM,name=jdbc/JackalopeDB to JNDI name 'java:/jdbc/JackalopeDB'
Hope it's helpful,
Frederic
 
Daniel Norin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Above posting helped me solve this problem :-) Thanks!!

Then I asked myself the question... so what about the jboss-web.xml?

It turned out as I suspected that instead of changing your jndi-references in your code, make the link in jboss-web.xml instead (that's actually what it's for).

In this case you would add:

<resource-ref>
<res-ref-name>jdbc/JackalopeDB</res-ref-name>
<jndi-name>java:/jdbc/JackalopeDB</jndi-name>
</resource-ref>

to your jboss-web.xml and this would save you from having to change in your code.
 
Daniel Norin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a final explanation to close this topic.

The problem of "Could not dereference DataSource object" seems to appear when the JNDI name actually exists but the resource references has a broken link, ie the name points on to another name that doesn't exist. When the name doesn't exist at all in the first place you'll get the "name not bound exception".
 
kitisak auttasran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.oracle-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi-name>
<connection-url>jdbc racle:thin:@localhost:1521:test2</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>test1</user-name>
<password>test1</password>
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
</datasources>

///////////////////////////////////////////////////////

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>WebModule1</display-name>
<resource-ref>
<res-ref-name>OracleDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
////////////////////////////////////////////
3.jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<resource-ref>
<res-ref-name>OracleDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/OracleDS</jndi-name>
</resource-ref>
</jboss-web>

//////////////////////////////////////////////
4.jsp code
<%
javax.naming.Context jndiCntx = new javax.naming.InitialContext();
out.println("Looking up OracleDS");
javax.sql.DataSource ds = (javax.sql.DataSource)jndiCntx.lookup("java:/OracleDS");
out.println("Found. Connecting to OracleDS");
java.sql.Connection con = ds.getConnection();
if (null != con) {
out.println("Connection successful!");
}
%>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic