2

请原谅这篇文章可能的格式不正确,因为我以前从未在这里发布过。我有一个运行的 Spring MVC Web 服务,它调用几个使用 JPA/hibernate 与 SQL Server 数据库交互的 DAO。Web 服务运行一段时间后,与数据库的连接似乎已关闭。

以下是其中一个 DAO 类的示例:

@Repository
public class OceanClientDaoImpl {

// Create entity manager objects that will talk to the database
private EntityManagerFactory entityManagerFactory;

/**
 * Load all clients in the database. Return as ClientLight objects.
 * 
 * @return List of ClientLight objects.
 */
public List<Client> loadClients() 
{
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    List<?> clients = 
            entityManager.createNativeQuery("select id, clientname, industry, logo, slug from client", 
                    Client.class).getResultList();
    entityManager.getTransaction().commit();
    entityManager.close();
    return Client.getCheckedList(clients);
}
}

entityManagerFactory 通过以下方式通过 spring 注入:

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
    <property name="driverClassName"     value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="url" />
    <property name="username" value="user" />
    <property name="password" value="pw" />
</bean>

<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="dataSource" ref="dataSource" />
</bean>

<context:component-scan base-package="com.theocean.dao">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Repository" />
</context:component-scan>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="oceanClientDaoImpl" class="com.theocean.dao.OceanClientDaoImpl">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

当我第一次启动应用程序时,这一切都很好。应用服务器是Tomcat。但是,在应用程序运行一段时间后,对服务器的请求开始收到此错误:

> SEVERE: Servlet.service() for servlet [spring] in context with path [/OceanWebService] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin failed: ] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server did not return a response. The connection has been closed.
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654)
    at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4844)
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6154)
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6106)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$1ConnectionCommand.doExecute(SQLServerConnection.java:1756)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectionCommand(SQLServerConnection.java:1761)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.setAutoCommit(SQLServerConnection.java:1901)
    at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:99)
    at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:58)
    at com.sun.proxy.$Proxy32.setAutoCommit(Unknown Source)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:87)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
    at com.theocean.dao.OceanClientDaoImpl.loadClients(OceanClientDaoImpl.java:52)
    at com.theocean.service.ExperienceClientService.loadClientsLight(ExperienceClientService.java:30)
    at com.theocean.web.ExperienceController.loadClientsLight(ExperienceController.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
SEVERE: Servlet.service() for servlet [spring] in context with path [/OceanWebService]     threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin failed: ] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server did not return a response. The connection has been closed.
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654)
    at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4844)
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6154)
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6106)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$1ConnectionCommand.doExecute(SQLServerConnection.java:1756)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectionCommand(SQLServerConnection.java:1761)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.setAutoCommit(SQLServerConnection.java:1901)
    at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:99)
    at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:58)
    at com.sun.proxy.$Proxy32.setAutoCommit(Unknown Source)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:87)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
    at com.theocean.dao.OceanClientDaoImpl.loadClients(OceanClientDaoImpl.java:52)
    at com.theocean.service.ExperienceClientService.loadClientsLight(ExperienceClientService.java:30)
    at com.theocean.web.ExperienceController.loadClientsLight(ExperienceController.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

因此,连接正在关闭。显然,无论是注入方法还是处理错误的方法等,实现都有问题。所以,问题是 - 这样做的正确方法是什么?如何避免连接关闭错误?提前非常感谢。

4

2 回答 2

3

我认为可能发生的情况是一些 DAO 在发生错误时使数据库事务处于打开状态。

上面的示例 DAO 正在对数据库事务进行编程管理,但不处理回滚场景。这是一个可以这样做的 DAO 实现示例:

    MyService {

   private EntityManagerFactory emf;

   public void myMethod() {
       EntityManager em = emf.createEntityManager();
       EntityTransaction tx = em.getTransaction();
       MyDaoA myDaoA = new MyDaoA(em);
       MyDaoB myDaoB = new MyDaoB(em);
       try {
           tx.begin();
           myDaoA.doSomething();
           myDaoB.doSomething();
           tx.commit();
       } catch(Exception e) {
           tx.rollback();
       }
    }
}

所有进行程序化事务管理的 DAO 都应该使用这种模式,否则事务迟早不会回滚,这可能会导致问题。

实现 DAO 的一种更频繁且更不容易出错的方法是仅直接注入一个实体管理器:

MyService {

   @PersistenceContext
   private EntityManager em;

   @Autowired
   private MyDaoA myDaoA;

   @Autowired
   private MyDaoB myDaoB;

   @Transactional
   public void myMethod() {
      myDaoA.doSomething();
      myDaoB.doSomething();
    }
}

注入 EntityManagerFactory 并进行手动事务管理应保留用于特殊情况,并且默认使用 @Transactional。

于 2013-11-08T00:12:54.183 回答
0

我想说问题在于Connection pooling,所以您可以使用c3p0连接池库来解决您的问题,但您应该非常小心连接池属性

实际上你可以做很多方法,我给你相同的代码供参考。它可能会有所帮助。

第一步:persistence.xml

<!-- c3p0 properties -->
        <!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted -->
        <property name="hibernate.c3p0.acquire_increment" value="1"/>
        <!-- If this is a number greater than 0, 
        c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds -->
        <property name="hibernate.c3p0.idle_test_period" value="60"/>

        <property name="hibernate.c3p0.max_size" value="20"/>
        <property name="hibernate.c3p0.max_statements" value="50"/>
        <property name="hibernate.c3p0.min_size" value="5"/>
        <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />

第 2 步:然后我们c3p0.properties在 src/main/resources/META-INF 下创建具有以下属性

c3p0.testConnectionOnCheckout=true
c3p0.preferredTestQuery=SELECT 1;

第三步:需要配置数据源

<bean id="myJdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
 <!-- Connection properties -->
 <property name="driverClass" value="$DS{database.class}" />
 <property name="jdbcUrl" value="$DS{database.url}" />
 <property name="user" value="$DS{database.username}" />
 <property name="password" value="$DS{database.password}" />
<bean/>
于 2013-11-08T11:12:50.433 回答