This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes JNDI error message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JNDI error message" Watch "JNDI error message" New topic
Author

JNDI error message

Abiodun Okusolubo
Ranch Hand

Joined: Apr 25, 2004
Posts: 72
Hello fellow ranchers, I need a support with the error message "java.lang.ClassCastException: org.jboss.resource.adapter.jdbc.WrapperDataSource"

Developing a webapp on
JBoss,Oracle Express,Windows XP,Hibernate framwork and using JBoss datasource for connections. The datasource connections works well without using Hibernate.

However, here is a section of hibernate.cfg.xml file:

<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.username"></property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="jndi.url"></property>
<property name="connection.datasource">
java:/comp/env/jdbc/MyUniConnection
</property>
<property name="connection.password"></property>
<property name="jndi.class"></property>


</session-factory>



The exception is thrown by,

(SessionFactory) new InitialContext().lookup("java:/comp/env/jdbc/MyUniConnection");

I have tried everything possible, I can make a connection with the use of Hibernate.
Please I need an assistance.

Cheers


SCJP, SCWCD, SCJD : in the making.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

(SessionFactory) new InitialContext().lookup("java:/comp/env/jdbc/MyUniConnection");


If you are looking up the SessionFactory then you will have to use the jndi name of the SessionFactory. Try this out:


[My Blog] [JavaRanch Journal]
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
I too got the same error message . I got the datasource object from looking up jndi . but how to get SessionFactory object from datasource object ,i dont know how to proceed

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


but how to get SessionFactory object from datasource object ,i dont know how to proceed

The DataSource doesn't provide the SessionFactory, you build a SessionFactory using a DataSource. The above example is binding this configured SessionFactory in JNDI and looking it up from there. Is this what you are trying to do? If it is, can you post your hibernate.cfg.xml and the code you use to do the lookup?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
iam trying get SessionFactory from datasource ...but how???. Iam getting the datasource object , but how to cast it to SessionFactory


/hibernate/generated/insurance.hbm.xml"/>

</session-factory>

</hibernate-configuration>


[/CODE]

[ July 12, 2007: Message edited by: mano ranjan ]
[ July 12, 2007: Message edited by: mano ranjan ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


iam trying get SessionFactory from datasource ...but how??? Iam getting the datasource object , but how to cast it to SessionFactory

This is something you can't do. A SessionFactory is created using a DataSource; they are different things, you can't cast from one to the other.

If you are using Hibernate, why are you looking up the DataSource at all? Looking at your hibernate.cfg.xml file, you are trying to bind your SessionFactory to java:comp/env/hibernate/SessionFactory in JNDI. Just look it up from there (as you have alreaddy done for your DataSource).
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
I tried as you said


and my code for look up is
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So your SessionFactory is bound in JNDI , but it is null. OK, look back through your logs to the point where you configured the SessionFactory, see if there are any exceptions being thrown.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
i cant get you . what you are saying . any way this my ds file which i deployed in my jboss server and my hibernate.cfg.xml




I have doubht in my configuration is that wright???

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, I think we are getting confused over trying to configure too much at once.

So, first a ]DataSource is a class that wraps the JDBC access to the database. Basically, it provides connections to a database. Because Hibernate is a bridge between your object model and the database it needs a DataSource to use for the low level JDBC stuff it will do. However, a DataSource is not part of Hibernate, and is not the same thing as a SessionFactory. If your DataSource configuration is OK (any execption in the logs?) and can look it up in JDNI we can foget about it.

A SessionFactory is a Hibernate class that provides Sessions. It does this based on a Configuration. That Configuration, is what your hibernate.cfg.xml file defines. The Configuration needs to know which DataSource to use. You tell it that with the property connection.datasource, e.g. :

It looks to me like you have this part right.

Hibernate will automatically bind your SessionFactory into JNDI if you use the element:

which you do, so that part looks OK.

So what is left? Well, Hibernate will bind the SessionFactory into JNDI after you have called the buildSessionFactory() method on you Configuration. If you are using the default behaviour, you will need a line of code that does this:

This will have had to happen before you try to lookup your SessionFactory from JNDI.

However, since you can lookup the SessionFactory, but get a NullPointerException, I'm guessing something has gone wrong in your Configuration so the SessionFactory could not be built. (if it wasn't in JNDI I'd expect a naming exception)

So, check your logs. When you configure your SessionFactory you will see lots of messages from Hibernate as it maps stuff. See if there is an error or exception message in there.
[ July 12, 2007: Message edited by: Paul Sturrock ]
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
iam getting a error message


thanks
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

21:35:09,078 INFO [STDOUT] 21:35:09,078 ERROR [InsuranceHome] Could not locate SessionFactory in JNDI


That clearly shows that the session factory was not bound to the JNDI. As Paul already pointed out, you will have to look into the logs to figure out what failed during the session factory creation. Look into the server.log file in %JBOSS_HOME%/server/default/log folder to see if there are any exceptions during JBoss startup or application deployment. If so, post the entire exception stacktrace.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
iam getting an error message in my log


Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That is telling you that the dialect must be set. Looking at your hibernate.cfg.xml it appears you do this, which makes me wonder if this is the hibernate.cfg.xml file you are using. Check htis is actually the file you ahve deployed into your application.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
thanks for your reply,I have one doubt i deleted my hibernate.cfg.xml file inorder to check the deployment.but now also its throwing the same error as above i dont know from where its exceuting..

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You deleted your hibernate.cfg.xml file, but your code seems to still be trying to create a SessionFactory. So, either reinstate your hibernate.cfg.xml file or remove the lines of code that try to create a SessionFactory (since that will not work if you don't have a configuration defined anywhere).
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
The changes which i did in hibernate.xml is not been affected in the server
when i deploy.Wheather i must include hibernate.properties file,because when i provide wrong information in hibernate.xml its showing the same old error which i except ... any file left out to configure . can you provide me some sample .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The changes which i did in hibernate.xml is not been affected in the server
when i deploy.

Do you mean the hibernate.cfg.xml is not being deployed top your server? If so, this sounds like an error in your build script or whatever mechanism you use to get your applciation to the server. Check this. The foolproof, though slow, way to make sure a change in the code in you IDE and what is deployed is to delete the deployed app and all temp directories from your JBoss instance, then rebuild.


Wheather i must include hibernate.properties file,because when i provide wrong information in hibernate.xml its showing the same old error which i except

Are you using hibernate.properties to provide your configuration, or hibernate.cfg.xml? Best pick which approach you want to use and just use that one, otherwise you will end up with different properties defines in different places, which will not help your effort to configure this!


can you provide me some sample

The hibernate.cfg.xml you posted above looks OK. Best procede with this, just try to work though the steps carefully. Hang in there, you are not far off!
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
Thanks for your reply,Ima very much confuced in this issue.How come the error will be same when delete the cfg file also i must throw hibernate.xml not found but its showing dialect exception.one more thing if i specify dialect also same error .. why so??? i deleted all temp file in jboss and redeployed with a new war file......
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


How come the error will be same when delete the cfg file also i must throw hibernate.xml not found but its showing dialect exception

Hibernate needs a minimum set of properties to function, the dialect being one. You can set these a number of ways: via a System property, in hibernate.properties or in hibernate.cfg.xml to name three. So Hibernate won't complain if it can't find hibernate.properties or hibernate.cfg.xml since neither of these files are mandatory, but it will complain if a required property has not been set.


one more thing if i specify dialect also same error .. why so??? i deleted all temp file in jboss and redeployed with a new war file...

My guess is the configuration file you think you are using is not being deployed. If you specify this property, hibernate will use it. How do you deploy your application?
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
Iam using eclipse ide so i use a option "run on server" and deploy . Even i checked the deployed file which has a hibernate.xml which whish has a property dialect


Is their any thing went wrong..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ok, that's odd. Your configuration file looks OK. Can you set the log level to debug for Hibernate? It should tell you what its doing - which might give some explanation of why its not working and you are not seeing an explainable exception message.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
how to set that property
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Just configure log4j to debug for all org.hibernate packages.

Look in %JBOSS_HOME%/server/[servername]/]conf for log4j.xml. Add a category for org.hibernate, and check your appenders are not defined using a Threshold higher than the category you define. I'd only send debug to your file appender, you are likely to get too much information to make the console viable. See this if you don't understand what I am talking about.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
i solved that part now in to new one.. just look at this my new code

but i get error saying

this is line were i get the error

[ July 16, 2007: Message edited by: David O'Meara ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, so you have changed how you intend to configure your SessionFactory to explicitly define the configuration in code. Well, at least this sorts your problems finding the configuration file, but it is limiting the application a bit since you now require a code change if you change a property.

If you are getting a ClassCastException looking up the transaction manager I'm guessing this is possibly some sort of class loader issue in JBoss. The same class loaded by two class loaders are treated as different types* hence ClassCastExceptions can occur. It's hard to say what to do without knowing how you package your applciation, but I'd clean your deploy directory down (make sure you remove everything you have deployed), delete temp. directries and try again. See if that fixs it. If it doesn't let us know and we can see if we can think of another idea.


*I think I've got that right - any JBoss employee moderators reading this please correct me if I'm wrong.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
Thanks for your reply ..This my mysql-ds.xml which i have deployed in jboss

then my exception is

Will you be able to figure out any thing by this code???

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

We've been though this before haven't we? The DataSource is not part of Hibernate. Assuming it is configured, we can forget about it. Your ClassCastException is not linked to this.

Becasue of the reason I gave in my last post, ClassCastExceptions can appear in JBoss where the class seems to be of the same type, but for the purposes of class loader checking is not. This sometimes happens when you have deployed things in different ways but failed to clean up after yourself. Have you tried removeing everything you have deploed to JBoss, deleting its temp directories and redeploying?
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
yes i tried as you said i deleted all tmp files stoped the server and started and then deployed a new war file . but my bad luck its the same class cast exception . what shall i do???

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Did you delete everything you had deployed before redeploying too?

What are the contents of your EAR or WAR file? What jars do you deploy with your app, and what Jars do you rely on JBoss to provide?
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
i deployed war file which contains my class file and html files and the needed library files are.
activation.jar
ajax4jsf-1.1.0.jar
antlr-2.7.6.jar
asm.jar
cglib-2.1.3.jar
commons-beanutils-1.7.0.jar
commons-codec-1.3.jar
commons-collections-3.1.jar
commons-digester-1.6.jar
commons-el-1.0.jar
commons-fileupload.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
commons-validator-1.2.0.jar
dom4j-1.6.1.jar
EasySI-0.9.0.jar
ehcache-1.2.4.jar
hibernate3.jar
jstl-1.1.0.jar
jta.jar
mysql-connector-java-5.0.3-bin.jar
myfaces-impl-1.1.4.jar
myfaces-api-1.1.4.jar
log4j.jar
mail.jar
mail-plugin.jar
myfaces-api-1.1.4.jar
myfaces-impl-1.1.4.jar
oscache-2.3.2.jar
oro-2.0.8.jar
portlet-api.jar
struts.jar
xerces.jar

i dont rellay on jboss jars in my appliaction

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

But you do deploy jta.jar. Why? JBoss already has a version of this in its classpath. My guess is this may be interfering with the class loading of some of the transaction management classes. Can you remove it, clean up your jboss instance (just like above) and try redeploying?
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
Thanks it worked.but now iam an exception on the next line

and my exception is


thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, so we are back where we started! Look at the logs. There must be a reason why the SessionFactory is not being configured and bound to JNDI. What exceptions or errors do you see before that exception is thown?
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
This is log which i got from server.log file in jboss


this is line were i get error

Object ds =(Object)jndiContext.lookup("java:/hibernate/SessionFactory");

thanks

[ July 13, 2007: Message edited by: mano ranjan ]
[ July 16, 2007: Message edited by: David O'Meara ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Object ds =(Object)jndiContext.lookup("java:/hibernate/SessionFactory");


Object ds =(Object)jndiContext.lookup("hibernate/SessionFactory");

Which are you using? Your code uses both. Make sure it is the former.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi
i tried in both the ways . but i get same null pointer exception.. any tips plzz
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Looking though your log, your SessionFactory is being correctly created and bound to JNDI at "java:/hibernate/MySessionFactory", not "java:/hibernate/SessionFactory". Change your lookup. Also, the configuration contains no mappings - is this what you want?
[ July 13, 2007: Message edited by: Paul Sturrock ]
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
HI
i have one doubt . by this line line of code
cfg.buildSessionFactory();
its returning us SessionFactory .Then why we need to lookup in JNDI , what is the need to lookup there .. why do we need a datasource object as we have sessionfactory object ...
Can you please answer this one

thanks
[ July 13, 2007: Message edited by: mano ranjan ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You don't. Binding the SessionFactory into JNDI is something you can choose to do if you want to. It makes sense to configure a SessionFactory once, since this is a heavy weight object, and reuse this instance. Since in JEE you have multiple JVMs the only real shared placed you can put this sort of stuff is in JNDI. If you know that your application will only ever run in one JVM (for example, it is a desktop application) then this step is unnecessary.
[ July 13, 2007: Message edited by: Paul Sturrock ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JNDI error message
 
Similar Threads
Sybase stored proc error
Hibernate NullPointerException
Hibernate not finding Oracle driver - despite straight JDBC working
getHibernateTemplate redux
JBoss AS5 with Hibernate 3 annotations