-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Could not find datasource hibernate exception
PostPosted: Fri Jan 09, 2009 11:40 pm 
Beginner
Beginner

Joined: Wed May 10, 2006 9:43 pm
Posts: 30
Location: Australia
Hi Everyone,

I have an error when I try to connect to a MySQL database in Hibernate using a JBOSS datasource.

I followed these instructions:

http://www.javabeat.net/tips/print.php?tips_id=99

Is there anything else i need?

I have used the reverse engineering functionality successfully and was able to load the database in the Hibernate reverse engineering editor when connecting to the database directly. But it wont work when I change the hibernate.cfg.xml file to point to the datasource in the JBOSS deploy directory.

Code:
<session-factory name="java:hibernate/SessionFactory">
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="connection.datasource">MySqlDS</property>


How does hibernate know which file contains the datasource defined in the hibernate.cfg.xml file ? The filename is mysql-ds.xml. Should the name of the datasource and the file name be the same except for the extension or is there another xml file i need to configure etc.?

The JBOSS datasource is configured appropriately i was able to query the database using a jsp as a test.


Thanks reading my post. If you could help i would appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2009 10:43 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
The filename doesn't matter. JBoss deploys the DataSource based on the contents of that file and from what you say, that much works. Try java:/MySqlDs for the data source name in the Hibernate config. If that doesn't work, post your whole config file as well as the error message you received when it failed.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2009 4:03 am 
Beginner
Beginner

Joined: Wed May 10, 2006 9:43 pm
Posts: 30
Location: Australia
Hello

The error still exists:

org.hibernate.HibernateException; Could not find datasource

javax.naming.NoInitialContextException: Need to specify class name in environment or system properly, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Hibernate.cfg.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="connection.datasource">java:/MySqlDS</property>
</session-factory>
</hibernate-configuration>


mysql-ds.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
   <local-tx-datasource>
      <jndi-name>MySqlDS</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/dbname</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <min-pool-size>1</min-pool-size>
      <user-name>root</user-name>
      <password>password</password>
      <metadata>
        <type-mapping>mySQL</type-mapping>
      </metadata>
  </local-tx-datasource>
</datasources>


Any suggestions? I must be missing a configuration file.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2009 12:32 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Where are you executing this code from? This will only work when running inside the JBoss instance. This error says you're missing a JNDI property which is supplied as default when you're inside JBoss. Are you trying to do this from an IDE or something?

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 7:36 pm 
Beginner
Beginner

Joined: Wed May 10, 2006 9:43 pm
Posts: 30
Location: Australia
Hello

Yes I'm using Eclipse 3.4.1 and the JBoss Tools Plugin 3.0.0 (which also includes the hibernate plugin etc).

Alyssa


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 6:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use a hibernate.properties to override application server dependent settings such as using a jdbc connection instead of datasource when running from commandline/eclipse.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 3:25 am 
Beginner
Beginner

Joined: Wed May 10, 2006 9:43 pm
Posts: 30
Location: Australia
Thanks for your response Max.

Could you provide a little more info I'm not sure what you mean by -

"override application server dependent settings such as using a jdbc connection instead of datasource when running from commandline/eclipse".

I'm realising that maybe reverse-engineering should only occur through references to mysql connection driver and url etc, instead of using a datasource. I was just trying to get everything working through jboss.

Alyssa


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 8:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you don't run inside jboss when reverse engineering so you can use jboss specific info.

you can use hibernate.properties to specify alternative hibernate properties that will override your values.

if you use jboss tools you can create a Seam project and see the hibernate-console.properties it creates as an example.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.