Error when lookup JNDI

Hi all,

I'm using Netbeans 5, Hibernate 3.2 cr2 and Jboss 4.0.3SP1, mysql 5

When I try to run the project from netbeans 5, I got the following error. Anything not right that cause the error?

08:05:53,246 INFO [STDOUT] 1111

08:05:53,246 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception

java.lang.ClassCastException: org.jboss.resource.adapter.jdbc.WrapperDataSource

So far from what I find out, it looks like the error happens at sessions = (SessionFactory)ctx.lookup(jndiName) line, because I don't see the output of "2222". Below is the function of getSessionFactory() that resides in my HibernateUtil.java

publicstatic SessionFactory getSessionFactory(){

SessionFactory sessions =null;

try{

Context ctx =new InitialContext();

String jndiName ="java:jdbc/cis";

System.out.println("1111");

sessions = (SessionFactory)ctx.lookup(jndiName);

System.out.println("2222");

}catch (NamingException ex){

thrownew InfrastructureException(ex);

}

return sessions;

}

Below is my mysql-ds.xml in Jboss

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml,v 1.3.2.1 2004/12/01 11:46:00 schrouf Exp $ -->

<!-- Datasource configfor MySQL using 3.0.9 available from:

http://www.mysql.com/downloads/api-jdbc-stable.html

-->

<datasources>

<local-tx-datasource>

<jndi-name>jdbc/cis</jndi-name>

<connection-url>jdbc:mysql://localhost:3309/test</connection-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>

<user-name>root</user-name>

<password>password</password>

<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>

<!-- sql to call when connection is created

<new-connection-sql>select count(*) from t_department</new-connection-sql>

-->

<!-- sql to call on an existing pooled connection when it is obtained from pool

<check-valid-connection-sql>select count(*) from t_department</check-valid-connection-sql>

-->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->

<metadata>

<type-mapping>mySQL</type-mapping>

</metadata>

</local-tx-datasource>

</datasources>

Really appreciate some help.

--

Thank you.

Regards,

Jap

[3296 byte] By [jbchurna] at [2007-10-3 2:58:31]
# 1

I can't tell you what is exactly wrong, but I can tell you how to figure out what's going on. The good thing is that your ctx.lookup is returnng something. The bad part is that you are not casting it to the right object type (i.e. it appears that the object returned by ctx.lookup is not of type SessionFactory. In you descriptor, it lookups like the object java:jdbc/cis is a DataSource, however, you haven't specified the data source class. Usually, when you create a datasource, you specify the class as java.sql.DataSource. Figure out where you can specify that in the configuration. I'm pretty sure that's the type returned by your context.lookup, so you could swap out SessionFactory for DataSource.

A clever way to solve this problem is to realise that ctx.lookup is returning an object. If you do

Object obj = ctx.lookup(jndiName);

System.out.println(obj.toString());

You can figure out two things: 1) the object exists in jndi, 2) you may be able to determne the class type of the object from what printed out.

queperknucklea at 2007-7-14 20:47:59 > top of Java-index,Core,Core APIs...