i'm stuck with this problem of datasource not found. i'm running the example in hibernate quickstart with tomcat. the one with Cat.hbm.xml.
i'm trying to use a jdbc driver (j connector) for a mysql 4.1.10a-nt database, and configuring tomcat to see the db as a datasource.
Hibernate version:
3.0
Mapping documents:
hibernate.cfg.xml, put in WEB-INF/classes directory of my app
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- net.sf.hibernate.connection.DatasourceConnectionProvider -->
<property name="connection.datasource">jdbc/quickstart</property>
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name="show_sql">false</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.provider_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">******</property>
<!-- Mapping files -->
<mapping resource="net/sf/hibernate/examples/quickstart/Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
server.xml of tomcat, only the section <Context> added befor the end of </Host>:
Code:
<Context path="/quickstart" docBase="quickstart" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/quickstart" auth="Container" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/quickstart">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/quickstart?relaxAutoCommit=true</value>
</parameter>
<parameter>
<name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>******</value>
</parameter>
<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
Code between sessionFactory.openSession() and session.close():i am trying to use the simple code suggested in the example, called HibernateUtil.java and i added a main() to test the app:
i am copying only the code with problems, in fact the error i get is on this line:
throw new ExceptionInInitializerError(ex);
Code:
(.......)
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
(.....)
public static void main(String[] args) {
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
tx.commit();
HibernateUtil.closeSession();
}
}
Full stack trace of any exception that occurs:
in bold the datasource problem:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
java.lang.ExceptionInInitializerError
at HibernateUtil.<clinit>(HibernateUtil.java:37)
Caused by: org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:48)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:80)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:349)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:58)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1509)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
at HibernateUtil.<clinit>(HibernateUtil.java:31)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:44)
... 6 more
Exception in thread "main"
Java Result: 1
Name and version of the database you are using:
mysql 4.1.10a-nt
The generated SQL (show_sql=true):
i don't think sql is yet generated
Debug level Hibernate log excerpt:
don't know how to find hibernate debug log, tell me if you do
maybe it doesn't find the hibernate.cfg.xml? do i have to put it in a different directory or in the classpath? i am not using hibernate.properties, is that a mistake? i don't think, since i'm using hibernate.cfg.xml.