Announcement Announcement Module
Collapse
No announcement yet.
Spring 4 and Hibernate 4. Connections not closed. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 4 and Hibernate 4. Connections not closed.

    Hi folks,

    thanks for the nice forum. Hope some of you can help me handling my problem.

    I try to implement an RESTful JSON Service running via Tomcat. Dependency injection is handled by Spring and JPA is implemented by Hibernate 4. The projects dependencies are managed via maven 3.
    Tomcat starts cleanly and I can send requests to the server which answeres with the correct information. So everything looks fine. BUT after a few requests the tomcat stops working and aften stopping the application server an exception is thrown looking like this one:
    Code:
     
     Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc Schwerwiegend: The web application [/fbo-rest-srv-rest-service] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads Schwerwiegend: The web application [/fbo-rest-srv-rest-service] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads Schwerwiegend: The web application [/fbo-rest-srv-rest-service] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks Schwerwiegend: The web application [/fbo-rest-srv-rest-service] created a ThreadLocal with key of type [org.springframework.core.NamedThreadLocal] (value [Request attributes]) and a value of type [org.springframework.web.context.request.ServletRequestAttributes] (value [[email protected]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak. Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks Schwerwiegend: The web application [/fbo-rest-srv-rest-service] created a ThreadLocal with key of type [org.springframework.core.NamedThreadLocal] (value [Locale context]) and a value of type [org.springframework.web.servlet.DispatcherServlet$1] (value [[email protected]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak. Apr 04, 2014 8:54:39 PM org.apache.coyote.AbstractProtocol stop Information: Stopping ProtocolHandler ["http-bio-8080"] log4j:WARN No appenders could be found for logger (org.hibernate.engine.jdbc.spi.SqlExceptionHelper). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Apr 04, 2014 8:54:39 PM org.apache.catalina.loader.WebappClassLoader loadClass Information: Illegal access: this web application instance has been stopped already.  Could not load org.hibernate.internal.util.JdbcExceptionHelper.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException 	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1588) [...] 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 	at java.lang.Thread.run(Thread.java:744)
    The exception tells me about problems with the database connection.

    Here some details of my configuration:
    My hiberante config.
    Code:
     
     <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 	xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  	<context:property-placeholder location="classpath:persistence-mysql.properties" />  	<bean id="sessionFactory" 		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 		<property name="dataSource" ref="dataSource" /> 		<property name="packagesToScan" value="de.bob.dpitest.entity" /> 		<property name="hibernateProperties"> 			<props> 				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 				<prop key="hibernate.dialect">${hibernate.dialect}</prop> 			</props> 		</property> 	</bean>  	<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource"> 		<property name="driverClassName" value="${jdbc.driverClassName}" /> 		<property name="url" value="${jdbc.url}" /> 		<property name="username" value="${jdbc.user}" /> 		<property name="password" value="${jdbc.pass}" /> 	</bean>  	<bean id="txManager" 		class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 		<property name="sessionFactory" ref="sessionFactory" /> 	</bean>  	<bean id="persistenceExceptionTranslationPostProcessor" 		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />  </beans>
    My web.xml
    Code:
     
     <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>DPITest Rest</display-name>  	<context-param> 		<param-name>log4jConfigLocation</param-name> 		<param-value>/WEB-INF/classes/log4j.properties</param-value> 	</context-param>  	<listener> 		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 	</listener>  	<servlet> 		<servlet-name>mvc-dispatcher</servlet-name> 		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 		<load-on-startup>1</load-on-startup> 	</servlet>  	<servlet-mapping> 		<servlet-name>mvc-dispatcher</servlet-name> 		<url-pattern>/rest/*</url-pattern> 	</servlet-mapping>  	<context-param> 		<param-name>contextConfigLocation</param-name> 		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 	</context-param>  	<listener> 		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 	</listener>  </web-app>
    My mvc-dispatcher-servlet.xml:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-4.0.xsd
    Index of /schema/mvc http://www.springframework.org/schem...spring-mvc.xsd Index of /schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <context:component-scan base-package="de.bob.dpitest.webapp.controller" />
    
    <import resource="classpath:META-INF/war-beans.xml" />
    <mvc:annotation-driven />
    
    </beans>
    And my generic dao
    Code:
     
     @SuppressWarnings("unchecked") public abstract class AbstractHibernateDao<T extends Serializable> implements 		IOperations<T> { 	private Class<T> clazz;  	@Autowired 	private SessionFactory sessionFactory;  	protected final void setClazz(final Class<T> clazzToSet) { 		clazz = Preconditions.checkNotNull(clazzToSet); 	}  	@Override 	public final T findOne(final long id) { 		return (T) getCurrentSession().get(clazz, id); 	}  	@Override 	public final List<T> findAll() { 		return getCurrentSession().createQuery("from " + clazz.getName()) 				.list(); 	}  	@Override 	public final void create(final T entity) { 		Preconditions.checkNotNull(entity); 		// getCurrentSession().persist(entity); 		getCurrentSession().saveOrUpdate(entity); 	}  	@Override 	public final T update(final T entity) { 		Preconditions.checkNotNull(entity); 		return (T) getCurrentSession().merge(entity); 	}  	@Override 	public final void delete(final T entity) { 		Preconditions.checkNotNull(entity); 		Session session = this.getCurrentSession(); 		session.delete(entity); 		session.flush(); 	}  	@Override 	public final void deleteById(final long entityId) { 		final T entity = findOne(entityId); 		Preconditions.checkState(entity != null); 		delete(entity); 	}  	protected final Session getCurrentSession() { 		return sessionFactory.openSession(); 	}  }
    My first try was to configure the connection pool. I enlarged it minimized it and played a little bit without success. But after a while i got an new exception containing this:
    Code:
     
     Timeout: Pool empty. Unable to fetch a connection in 5 seconds, none available[size:15; busy:15; idle:0; lastwait:5000].
    So in my view the connection pool is not the problem. Busy 15 of 15 says you are not closing your connections to me. Do you agree? But I don't see the configuration problem.

    My question to is what to do you thing what could I also try to find my problem?

    Thanks for your ideas!

  • #2
    I have forgotten the complete transaction manaagement.

    In hibernate config I had to add:
    Code:
    <tx:annotation-driven transaction-manager="txManager" />
    Also all DAOs needed the @Transactional Annotation.

    Solved.

    Comment

    Working...
    X