Hibernate and log4j config under Tomcat.

R

Rico

"Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
WEB-INF/classes directory of your web-applications. Log4j will find the
properties file and initialize itself. This is easy to do and it works."

"The XML configuration file is placed in the context classpath
(WEB-INF/classes), as hibernate.cfg.xml"

I don't know what's Tomcat's problem but in both cases, despite what the
documentation says, placing the config files in WEB-INF/classes didn't
work.
For log4j I ended up using an initialization servlet.
For Hibernate, I placed the xml file in several folders after some
googling:

WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
<tomcat_home>/conf

Nothing worked and this time Tomcat explicitly told me that the file is
not to be found. I ended up having to specify an absolute path to at least
see that the code I'm actually interested in works.

I don't know what's Tomcat's problem... or mine...

Rico.
 
R

Ryan Stewart

Rico said:
"Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
WEB-INF/classes directory of your web-applications. Log4j will find the
properties file and initialize itself. This is easy to do and it works."

"The XML configuration file is placed in the context classpath
(WEB-INF/classes), as hibernate.cfg.xml"

I don't know what's Tomcat's problem but in both cases, despite what the
documentation says, placing the config files in WEB-INF/classes didn't
work.
For log4j I ended up using an initialization servlet.
For Hibernate, I placed the xml file in several folders after some
googling:

WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
<tomcat_home>/conf

Nothing worked and this time Tomcat explicitly told me that the file is
not to be found. I ended up having to specify an absolute path to at least
see that the code I'm actually interested in works.

I don't know what's Tomcat's problem... or mine...

Rico.
Likely yours. I just set up a fresh Hibernate install on Tomcat 5.5 (with Log4j)
with no problems. Define what you mean by "didn't work".
 
R

Rico

Likely yours. I just set up a fresh Hibernate install on Tomcat 5.5
(with Log4j) with no problems. Define what you mean by "didn't work".

As I said, for Hibernate under Tomcat (4.1.18 and 4.1.31), no matter where
I put the hibernate.cfg.xml, including WEB-INF/classes, I got the message
that the file is not to be found when we reach the following code:

sessionFactory = new Configuration().configure()
.buildSessionFactory();

2005-02-18 15:28:00,968 WARN http-8080-Processor4
net.sf.hibernate.cfg.Configuration - /hibernate.cfg.xml not found

I've considered trying Tomcat 5.0.x but it seems that first I need to
figure out which ones and adapt a couple of settings in server.xml and/or
web.xml to get it to stop crashing.

Rico.
 
R

Ryan Stewart

Rico said:
As I said, for Hibernate under Tomcat (4.1.18 and 4.1.31), no matter where
I put the hibernate.cfg.xml, including WEB-INF/classes, I got the message
that the file is not to be found when we reach the following code:

sessionFactory = new Configuration().configure()
.buildSessionFactory();

2005-02-18 15:28:00,968 WARN http-8080-Processor4
net.sf.hibernate.cfg.Configuration - /hibernate.cfg.xml not found

I've considered trying Tomcat 5.0.x but it seems that first I need to
figure out which ones and adapt a couple of settings in server.xml and/or
web.xml to get it to stop crashing.
What version of Hibernate are you using? When I remove the config file, I get
this in the log: (line breaks inserted to prevent wrap and logging set to INFO
level)
INFO [http-8080-Processor25] cfg.Environment (<clinit>:483)
- Hibernate 2.1.8
INFO [http-8080-Processor25] cfg.Environment (<clinit>:512)
- hibernate.properties not found
INFO [http-8080-Processor25] cfg.Environment (<clinit>:543)
- using CGLIB reflection optimizer
INFO [http-8080-Processor25] cfg.Environment (<clinit>:572)
- using JDK 1.4 java.sql.Timestamp handling
INFO [http-8080-Processor25] cfg.Configuration (configure:909)
- configuring from resource: /hibernate.cfg.xml
INFO [http-8080-Processor25] cfg.Configuration
(getConfigurationInputStream:881)
- Configuration resource: /hibernate.cfg.xml
WARN [http-8080-Processor25] cfg.Configuration
(getConfigurationInputStream:885)
- /hibernate.cfg.xml not found
ERROR [http-8080-Processor25] persistence.HibernateUtil (<clinit>:19)
- Initial SessionFactory creation failed.
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found

Does that look like what you're getting? I assume you're using some build tool.
Are you sure that the config file is being built to the right place?
 
R

Rico

What version of Hibernate are you using? When I remove the config file,
I get this in the log: (line breaks inserted to prevent wrap and logging
set to INFO level)
INFO [http-8080-Processor25] cfg.Environment (<clinit>:483)
- Hibernate 2.1.8
INFO [http-8080-Processor25] cfg.Environment (<clinit>:512)
- hibernate.properties not found
INFO [http-8080-Processor25] cfg.Environment (<clinit>:543)
- using CGLIB reflection optimizer
INFO [http-8080-Processor25] cfg.Environment (<clinit>:572)
- using JDK 1.4 java.sql.Timestamp handling
INFO [http-8080-Processor25] cfg.Configuration (configure:909)
- configuring from resource: /hibernate.cfg.xml
INFO [http-8080-Processor25] cfg.Configuration
(getConfigurationInputStream:881)
- Configuration resource: /hibernate.cfg.xml
WARN [http-8080-Processor25] cfg.Configuration
(getConfigurationInputStream:885)
- /hibernate.cfg.xml not found
ERROR [http-8080-Processor25] persistence.HibernateUtil (<clinit>:19)
- Initial SessionFactory creation failed.
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found

Does that look like what you're getting? I assume you're using some
build tool. Are you sure that the config file is being built to the
right place?

I was using 2.1.6 and I upgraded to 2.1.8. The above is almost exactly
what I still get. I am positive that I have a file hibernate.cfg.xml in my
application's WEB-INF/classes folder.
It's the same config file that was at the root of the Eclipse project that
didn't require an absolute path to find it.

Rico.
 
R

Rico

"Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
WEB-INF/classes directory of your web-applications. Log4j will find the
properties file and initialize itself. This is easy to do and it works."

"The XML configuration file is placed in the context classpath
(WEB-INF/classes), as hibernate.cfg.xml"

I don't know what's Tomcat's problem but in both cases, despite what the
documentation says, placing the config files in WEB-INF/classes didn't
work.
For log4j I ended up using an initialization servlet.
For Hibernate, I placed the xml file in several folders after some
googling:

WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
<tomcat_home>/conf

Nothing worked and this time Tomcat explicitly told me that the file is
not to be found. I ended up having to specify an absolute path to at least
see that the code I'm actually interested in works.

I don't know what's Tomcat's problem... or mine...

Rico.

Believe me, I've really had to keep trying before getting to this point:

Tomcat can now see hibernate.cfg.xml and log4j.properties which are in my
WEB-INF/classes folder as per the documentation.
What isn't in the documentation is whether the absolute path for the web
application needs to be
<CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes

because from what I observe, only then things work as they are supposed to.
If I am to use another installation path for the application, I'm stuck
with e.g using initialization servlet or probably a ResourceBundle to pass
the path to Hibernate's Configuration class ?

Rico.
 
R

Ryan Stewart

Rico said:
Believe me, I've really had to keep trying before getting to this point:

Tomcat can now see hibernate.cfg.xml and log4j.properties which are in my
WEB-INF/classes folder as per the documentation.
What isn't in the documentation is whether the absolute path for the web
application needs to be
<CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes
No, it does not.
because from what I observe, only then things work as they are supposed to.
If I am to use another installation path for the application, I'm stuck
with e.g using initialization servlet or probably a ResourceBundle to pass
the path to Hibernate's Configuration class ?
What do you mean by "another installation path"? What path, and how do you set
it up for Tomcat to access it?
 
R

Rico

No, it does not.

What do you mean by "another installation path"? What path, and how do
you set it up for Tomcat to access it?

C:\\myWebApp

<Context path="/something" docBase="C:\\myWebApp" />

In such a case, Tomcat cannot find
C:\myWebApp\WEB-INF\classes\hibernate.cfg.xml

Same goes for
C:\myWebApp\WEB-INF\classes\log4j.properties

Though from the way I understand the documentation they're supposed to be
found.
It's when I use the following installation folder that e.g
Hibernate's no-args configure() succeeds as Tomcat can find the
hibernate.cfg.xml in:

<CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes

Rico.
 
R

Ryan Stewart

Rico said:
humm... any particular reason why we'd expect that to make a difference?

Not particularly. It's just an inconsistency that I noticed. This is an OS path,
not a Java String. You don't need to escape the '\'s. It *should* work the same
regardless, but you never know. As for your problem in general, either you've
got a corrupted installation or you're not telling some vital bit of
information, because the behavior you've described is not normal. Here is a
complete context that I have saved as
"$CATALINA_BASE\conf\Catalina\localhost\recipes.xml" on WinXP Home:
<Context docBase="C:\Projects\recipes\target\recipes"
reloadable="true"
backgroundProcessorDelay="2">
<Resource name="jdbc/recipes" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="2" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/reciperack"/>
</Context>

Obviously you can ignore the Resource element. The corresponding exploded web
application is at "C:\Projects\recipes\target\recipes", just as in the docBase
attribute. In "C:\Projects\recipes\target\recipes\WEB-INF\classes", I have
hibernate.cfg.xml and log4j.properties, not to mention a Struts resource file,
jndi.properties, and hibernate mapping file, all of which are found on the
classpath when needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,991
Messages
2,570,217
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top