Hibernate sessionfactory在一段时间后抛出nullpointer异常

时间:2017-05-24 14:32:51

标签: java hibernate spring-mvc servlet-filters

在我的春季mvc应用程序中。为了启用spring @Transactional功能,我将transactionManager bean放在spring-servlet.xml中。现在我能够回滚该事务。但是经过一段时间后,sessionfactory对象抛出了nullpointer异常。我想找到里面发生的事情。请分享您的建议。我的spring和hibernate配置如下:

弹簧servlet.xml中

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd           
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    default-lazy-init="false">

    <!-- <mvc:resources mapping="/resources/**" location="/resources/" /> -->
    <mvc:annotation-driven/>
    <context:annotation-config />
    <context:component-scan base-package="com.sample.myproject" />
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="false" />   
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <mvc:interceptors>
        <bean class="com.sample.myproject.user.interceptor.UserInterceptor">
            <property name="welcome" value="./welcome"/>
        </bean>
    </mvc:interceptors>

    <!-- <bean id="viewResolver" 
            class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
     -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
          <entry key="html" value="text/html"/>
          <entry key="json" value="application/json"/>
        </map>
      </property>
      <!--  p:redirectHttp10Compatible="false" -->
      <property name="viewResolvers">
        <list>
          <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
            p:viewClass="org.springframework.web.servlet.view.JstlView"
            p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp"
          />
        </list>
      </property>
      <property name="defaultViews">
        <list>
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
            <property name="prefixJson" value="true"/>
          </bean>
        </list>
      </property>
    </bean>

</beans>

的applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd           
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    default-lazy-init="false">

    <!-- This required so that Spring can recognize our annotated beans -->

    <!-- This required so that Spring can recognize certain annotations,
     For example @Controller and @Service. Make sure you set the correct base-package
    -->
    <!-- <context:component-scan base-package="com.turborep.turbotracker" /> -->

    <!-- 
    This is responsible for automatically converting our custom POJO to JSON. 
    Make sure you have Jackson in your classpath
    -->
    <mvc:annotation-driven />
    <import resource="hibernate-context.xml" />
</beans>

冬眠-context.xml中

<?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:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            ">

    <context:property-placeholder location="/WEB-INF/spring.properties" />

    <bean id="userServiceDao" class="com.sample.myproject.system.service.SysServiceImpl" >
    </bean>

    <bean id="sysServiceDao" class="com.sample.myproject.user.service.UserServiceImpl" >
    </bean>

    <!-- Enable annotation style of managing transactions -->

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.sample.myproject"/>

    <!-- Declare a datasource that has pooling capabilities-->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close"
                p:driverClass="${app.jdbc.driverClassName}"
                p:jdbcUrl="${app.jdbc.url}"
                p:user="${app.jdbc.username}"
                p:password="${app.jdbc.password}"
                p:acquireIncrement="5"
                p:idleConnectionTestPeriod="60"
                p:maxPoolSize="2500"
                p:maxStatements="5000"
                p:minPoolSize="3" 
                p:testConnectionOnCheckout="true"
                p:maxConnectionAge="300"/>

</beans>

请分享您的建议以解决此问题。

1 个答案:

答案 0 :(得分:1)

我认为如果你将applicationContext.xml代码更改为这样的东西并在服务层中使用@Transactional注释,它将按照需要工作:

<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="${get.driverClass}" />
    <property name="jdbcUrl" value="${get.jdbcUrl}" />
    <!--  
          This option allow the Hibernate create database on startup.
          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/java_arabic_community?createDatabaseIfNotExist=true&amp;
          useUnicode=yes&amp;characterEncoding=utf8" /> 
    -->
    <property name="user" value="${get.user}" />
    <property name="password" value="${get.password}" /> 

    <!-- these are connection pool properties for C3P0 -->
    <property name="minPoolSize" value="3" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
</bean>  

<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.community.web" />
    <property name="hibernateProperties">
       <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <!--  
            This option allow the Hibernate create tables on startup and destroy previous data
             <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>
          <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager" />