Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Close connection to database correctly
PostPosted: Sun Mar 25, 2012 5:43 am 
Newbie

Joined: Tue Jan 31, 2012 3:23 pm
Posts: 7
Hi Guys,

I'm currently experiencing an interesting problem.

My Situation:
- I am currently developing a web service (if you want further details, I'm using VAADIN for programming with JAVA)
- My database behind is java derby
- I am using hibernate (Of course :) )
- I'm currently deploying it on Tomcat v7.0

My Problem:
- When I change something in my code (doesn't matter what), the server should reload it without the need of being restarted
- The server reloads the application successfull, but if I try to click on something (so after the reloading) I get an error

Error Message:
Quote:
Cause: org.hibernate.exception.GenericJDBCException: Could not open connection] with root cause
ERROR XSDB6: Another instance of Derby may have already booted the database C:\HTML-Ausgabe\database\DocumentDB.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
...


My Thoughts on this
It seems that somehow on the reloading process the connection/context to hibernate doesnt get destroyed/closed and so the error occures when the server tries to reconnect to the database

My Code
I have a class, called Hibernate Listener:

Code:
public class HibernateListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        HibernateUtil.getSessionFactory(); // Just call the static initializer of that class   
    }
   
    public void contextDestroyed(ServletContextEvent event) {
        HibernateUtil.getSessionFactory().close(); // Free all resources
    }
}


My 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="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
      <property name="hibernate.connection.url">jdbc:derby:C:\HTML-Ausgabe\database\DocumentDB;create=true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
      <property name="hibernate.current_session_context_class">thread</property>
      <property name="hibernate.hbm2ddl.auto">create-drop</property>
      <property name="hibernate.show_sql">true</property>
      
      <mapping class="view.model.database.User"/>
      <mapping class="view.model.database.Document"/>
      <mapping class="view.model.database.Version"/>
      <mapping class="view.model.database.VersionData"/>
   </session-factory>
</hibernate-configuration>


My (VAADIN) web.xml, in which I added a "listener" for the upper shown HibernateListener (check the bold text below):

Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>Bachelorprojekt</display-name>
   <context-param>
      <description>Vaadin production mode</description>
      <param-name>productionMode</param-name>
      <param-value>false</param-value>
   </context-param>
   <servlet>
      <servlet-name>Bachelorprojekt Application</servlet-name>
      <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
      <init-param>
         <description>Vaadin application class to start</description>
         <param-name>application</param-name>
         <param-value>view.view.WebsiteFrame</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>Bachelorprojekt Application</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>
   
[b]   <listener>
      <listener-class>view.model.database.HibernateListener</listener-class>
   </listener>[/b]
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
</web-app>


If anyone of you could help me somehow, I would be really happy. Currently I dont know what to change to stop this error happening. And, of course, I cant always restart the whole server later when my application is on the internet, if I change one line of code.

Thanks a lot for every answer and thought that you're sharing with me.


Top
 Profile  
 
 Post subject: Re: Close connection to database correctly
PostPosted: Mon Mar 26, 2012 6:26 am 
Newbie

Joined: Tue Jan 31, 2012 3:23 pm
Posts: 7
Can anyone help me please here, this is a serious issue for me at the moment :(


Top
 Profile  
 
 Post subject: Re: Close connection to database correctly
PostPosted: Tue Mar 27, 2012 6:12 am 
Newbie

Joined: Tue Jan 31, 2012 3:23 pm
Posts: 7
Can anyone help me here?

Or did someone experience something similiar while using a database?

I guess if you use a database you also don't restart your whole server from scratch if you changed one line of code, do you?

Thanks a lot for every post


Top
 Profile  
 
 Post subject: Re: Close connection to database correctly
PostPosted: Fri Mar 30, 2012 7:36 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
You're closing the SessionFactory, in _contextDestroyed_ but you're not restarting it in _contextInitialized_ as the static initializer triggers only the first time.

Don't use HibernateUtil.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.