当前位置: 首页 > 面试题库 >

org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@779de014已关闭

公西星海
2023-03-14
问题内容

我正在尝试使用Hibernate
5创建或更新用户,并且抛出以下异常:org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@779de014已关闭

我正在使用以下技术:

  • Spring 4

    *Dormancy 5

    *Marvin

    *Erosion

这是我的UserDaoImpl.Java

package com.fasttrack.users.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.fasttrack.users.model.User;

@Repository
@Transactional
public class UserDaoImpl extends AbstractDao implements UserDao
{
    @Autowired
    private SessionFactory sessionFactory;
    public boolean saveUser(User user)
    {
         Session session=sessionFactory.getCurrentSession();
          try
          {
             session.persist(user);
             return true;
          }

          catch (Exception e) 
          {

            e.printStackTrace();
            return false;
        }

          finally 
          {
              session.close(); 
           }

    }


    @SuppressWarnings("unchecked")
    public List<User> getUsers()
    {
         List<User> users;
         Session session=sessionFactory.getCurrentSession();
          try
          {
              users=(List<User>) session.createQuery("FROM USER"); 
              return users;
          }

          catch (Exception e) 
          {
            e.printStackTrace();
        }

          finally {
              session.close(); 
           }
        return null;
    }

    public User getUser(String id)
    {
         User user;
         Session session=sessionFactory.getCurrentSession();
          try
          {
              user=(User)session.get(User.class,id); 
              return user;
          }

          catch (Exception e) 
          {
            e.printStackTrace();
            return null;
        }

          finally 
          {
              session.close(); 
           }

    }



    public boolean updateUser(User user)
    {
        Session session = sessionFactory.getCurrentSession();
        try
            {
                session.saveOrUpdate(user);
                return true;
            }

        catch (Exception e)
            {
                e.printStackTrace();
                return false;
            }

        finally
            {
                session.close();
            }
    }


    public boolean deleteUserById(String id)
    {
        User user = new User();
        user.setId(id);

        Session session = sessionFactory.getCurrentSession();
        try
            {
                session.delete(user);
                return true;
            }

        catch (Exception e)
            {
                e.printStackTrace();
                return false;
            }

        finally
            {
                session.close();
            }
    }

}

这是我的TestUserDao.java

import com.fasttrack.users.dao.UserDao;
import com.fasttrack.users.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/Hibernate-test.xml")

public class TestUserDao
{
    @Autowired(required=true)
    public UserDao userDao;

    @Test
    public void testSaveUser()
    {
        User user=new User();
        user.setFirstName("TestUser1");
        boolean result=userDao.saveUser(user);
    }
}

错误信息:

Mar 01, 2017 1:14:52 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
Mar 01, 2017 1:14:52 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6be46e8f, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3567135c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@327471b5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4157f54e, org.springframework.test.context.transaction.TransactionalTestExecutionListener@90f6bfd, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@47f6473]
Mar 01, 2017 1:14:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Hibernate-test.xml]
Mar 01, 2017 1:14:53 PM org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@2a5ca609: startup date [Wed Mar 01 13:14:53 EST 2017]; root of context hierarchy
Mar 01, 2017 1:14:53 PM org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [database.properties]
Mar 01, 2017 1:14:53 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Mar 01, 2017 1:14:53 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.6.Final}
Mar 01, 2017 1:14:53 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 01, 2017 1:14:53 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 01, 2017 1:14:54 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Mar 01, 2017 1:14:54 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Mar 01, 2017 1:14:54 PM org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@130d63be] of Hibernate SessionFactory for HibernateTransactionManager
Generated Id: USER0
Mar 01, 2017 1:14:55 PM org.springframework.orm.hibernate5.HibernateTransactionManager doRollbackOnCommitException
SEVERE: Commit exception overridden by rollback exception
java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@6b410923 is closed
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:128)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:247)
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:79)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:221)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
    at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:582)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy31.saveUser(Unknown Source)
    at TestUserDao.testSaveUser(TestUserDao.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Hibernate 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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <context:property-placeholder location="classpath:database.properties" />
      <context:component-scan  base-package="com.timelee.*" />


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


     <!--  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  -->

     <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>

    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.timelee.timesheet.model</value>
                <value>com.timelee.users.model</value>
                <value>com.timelee.*</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>       
    </bean>

    <bean id="transactionManager"  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

问题答案:

我在您的错误消息“ 严重提交异常被回滚异常覆盖
”中发现了一些奇怪的东西。如果查看UserDaoImpl.Java文件中的try块,则具有return语句,该语句在返回之前关闭休眠连接。而且你必须session.close()在finally块声明,没有任何意义,因为连接已经关闭。您介意删除session.close()声明,然后重试。让我知道这是否可以解决您的问题。



 类似资料:
  • 我试图用Hibernate5创建或更新用户,它抛出以下异常:org。冬眠资源jdbc。内部的LogicalConnectionManagedImpl@779de014关门了 我正在使用以下技术: Spring4 冬眠5 马文 蚀 这是我的: 这是我的: 错误消息: Hibernatexml文件:

  • 我在springboot应用程序中使用webclient来调用外部restful web服务。间歇性地获取此异常。 在收到此异常之前,我在日志中看到以下警告。 这是完整的堆栈跟踪: 这是MessageServiceImpl.java 这里是客户端组件类 在下面的配置中,我在SSLContext中添加了信任存储。 下面是应用程序属性文件 我已经尝试通过下面链接中给出的解决方案来解决这个问题,但是没有

  • 我想使用stream getfirst方法两次,但是出现了一个错误,即(java.lang.IllegalStateException:stream已经被操作或关闭),并且这个流代码以此处命名的注释开始。

  • 我读了一个关于docker的很好的问题--答案概述了docker的实现细节。我想知道在Windows平台上是否可以做类似的事情。 Docker的Windows替代方案是否存在? 理论上是否可以使用其他(基于Windows的)组件来构建它? 更新1: 稍微相关的问题(沙箱):Windows平台是否有轻量级、可编程的沙箱API? 更新2:: 有关如何在windows上安装docker的信息(无关)-官

  • 所以我的问题是:我通常把我的笔记本电脑连接到外部屏幕上,一切正常,直到我需要把我的笔记本电脑带到大学去做进度报告。当我的笔记本电脑没有连接到外部屏幕时,imshow显示的窗口在底部被切断,如图所示。 以前有人遇到过这个问题吗?如果有,你能分享如何解决这个问题吗?我的笔记本电脑使用windows 10,python 3.6.4和opencv 3.3.0。 我当前的代码有点长,所以我将在这里给出流程:

  • 想改进这个问题吗 通过编辑此帖子,更新问题,使其只关注一个问题。 ** ** 之间的所有文本都已弃用。我不知道应该用什么来替换它。你能帮我一些忙吗?我是初学者,只是制作一个应用程序来与我的树莓派交流。 公共类JSONParser {