-->
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.  [ 4 posts ] 
Author Message
 Post subject: EntityManager 3.2.0GA / jar locking / PersistenceXmlLoader
PostPosted: Thu Nov 30, 2006 7:14 pm 
Newbie

Joined: Thu Nov 30, 2006 6:09 pm
Posts: 9
I am writing this to document two resolutions to a problem I have found while running EntityManager within a servlet container on Windows and the way that PersistenceXmlLoader.java within EntityManager searches for persistence_1_0.xsd (NOT persistence.xml).

This is specific to windows, and its file locking behaviour.

Environment: Windows XP SP2, JDK 1.5.0_05, Hibernate-everything at 3.2.0GA, Tomcat 5.5.12

Problem: When undeploying a webapp that uses EntityManager, the EntityManager.jar within the webapp is locked and cannot be deleted. This results in a failure to undeploy the webapp. If the servlet container (Tomcat) is shut down, the lock is released and the jar can be deleted.

PersistenceXmlLoader.java involvement: This class appears to be involved in searching for persistence_1_0.xsd on the classpath using a url. However it does not set caching to false before doing the configURL.openStream() call. This means that all the jars that it opens get locked by windows.

Root cause: Jar access using a URL with caching enabled. See:
http://forum.springframework.org/archive/index.php/t-19516.html
http://tomcat.apache.org/faq/windows.html#lock

Resolution #1:
Copy persistence_1_0.xsd out of EntityManager.jar, and place it in WEB-INF/classes. This should cause the xml loading code to find it first, and not search any of the jars.

Resolution #2:
Make sure the following code is ran before instantiating the EntityManager (can use ServletContextListener for example):
Code:
    try
   {
      if(System.getProperty("os.name").contains("Windows"))
         new URL("http://This-is-just-to-get-the-URLConnection").openConnection().setDefaultUseCaches(false);
   }
   catch (IOException e)
   {
      e.printStackTrace();
   }


This is a vm-wide setting so it will affect all url connections. Why isn't this code cleaner? See:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4851466
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4405789

I hope this helps anyone else in the same situation!
Maybe the code could also be in EntityManager itself? I'll leave that to the dev team.

Jim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 6:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://opensource.atlassian.com/projects/hibernate/browse/EJB-256

I've added in PersistenceXmlLoader.loadURL

Code:
if (configURL != null) {
         URLConnection conn = configURL.openConnection();
         conn.setUseCaches( false ); //avoid JAR locking on Windows and Tomcat
         is = conn.getInputStream();
      }


Let me know if that fixes your issue.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 1:45 pm 
Newbie

Joined: Thu Nov 30, 2006 6:09 pm
Posts: 9
Yes, that will do the trick.

I like the use of setUseCaches() for this particular URLConnection because it avoids having Hibernate affect the entire vm. I used setDefaultUseCaches() simply to avoid modifying the many jars I am using.

Setting caching to false for the URLConnection may have a minor performance hit though, so it wouldn't hurt to profile this change. That is also why I have
Code:
if(System.getProperty("os.name").contains("Windows"))
in my code. I am isolating the change to the OS where the problem occurs.

Jim


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 3:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't expect any serious perf penalty since this URL will not be reused beyond Persistent Unit initialization.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.