lateralus
lateralus

Reputation: 1030

Hibernate and Spring sessionFactory null and db not created

I get some problem while trying to integrate Spring with Hibernate. From the log it seems the tables and the database is created, but I can't find the database file and while inspecting the sessionFactory I get nullpointer exception, and I can't really understand what's going on.

These are my configuration files:

<tx:annotation-driven />

<context:component-scan base-package="org.fabrizio.fantacalcio.controller"/>

In my BaseDaoImpl I have the following code:

@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {

//  private static final Logger logger = LoggerFactory
//          .getLogger(BaseDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

The log generates these messages, resulting in null sessionFactory and no db file generated:

18/06/2015 11:20:49 - DEBUG - (StatisticsInitiator.java:110) - Statistics initialized [enabled=true]
18/06/2015 11:20:54 - DEBUG - (SessionFactoryImpl.java:1339) - HHH000031: Closing
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:149) - The mbean com.mchange.v2.c3p0:type=PooledDataSource,identityToken=2saucp9a1nmzsq198qnlq|1bdc6b2 was not found in the registry, so could not be unregistered.
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:97) - C3P0Registry mbean unregistered.
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (AbstractPoolBackedDataSource.java:477) - com.mchange.v2.c3p0.PoolBackedDataSource@1bdc6b2 has been closed. 
java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:477)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:489)
    at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:372)
    at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:348)
    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.stop(C3P0ConnectionProvider.java:258)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:377)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:361)
    at org.hibernate.cfg.Configuration$2.sessionFactoryClosed(Configuration.java:1924)
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryClosed(SessionFactoryObserverChain.java:65)
    at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1376)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:488)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:258)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:900)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:907)
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:908)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:884)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5035)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5687)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1591)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1580)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
18/06/2015 11:20:54 - DEBUG - (NewPooledConnection.java:646) - com.mchange.v2.c3p0.impl.NewPooledConnection@9be45 closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
    at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:646)
    at com.mchange.v2.c3p0.impl.NewPooledConnection.closeMaybeCheckedOut(NewPooledConnection.java:259)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:619)
    at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:1024)
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1049)
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1010)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1260)
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:627) - Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1027) - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (BootstrapServiceRegistryImpl.java:308) - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
18/06/2015 11:20:54 - DEBUG - (AbstractServiceRegistryImpl.java:406) - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries

If someone could review my xml code formatting I would appreciate, as It doesn't show properly.

EDIT: This is UtenteDaoImpl class, it does nothing as it inherits stuff from BaseDao.

@Repository
public class UtenteDaoImpl extends BaseDaoImpl<Utente> implements UtenteDao{

}

Upvotes: 0

Views: 1454

Answers (2)

user4610470
user4610470

Reputation:

may be useful for you and solve your problem spring and hibernate both are combination this 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"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />

    <mvc:annotation-driven />

    <context:component-scan base-package="com.WordOfTheDay" />

    <mvc:interceptors>
        <bean id="webContentInterceptor"
            class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0" />
            <property name="useExpiresHeader" value="false" />
            <property name="useCacheControlHeader" value="true" />
            <property name="useCacheControlNoStore" value="true" />
        </bean>
    </mvc:interceptors>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/Messages" />
        <property name="cacheSeconds" value="3000" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.WordOfTheDay"></property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://127.11.115.2:3306/spiritualthesurus?characterEncoding=UTF-8"
        p:username="admin2BXQEV1" p:password="8PcKcsgLWUMR" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000000" />
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
</beans> 

Upvotes: 0

codeaholicguy
codeaholicguy

Reputation: 1691

Please change sessionFactory to protected.

@Autowired
protected SessionFactory sessionFactory;

Upvotes: 0

Related Questions