当前位置: 首页 > 知识库问答 >
问题:

Springboot应用程序在数据库重新启动时失败

柴磊
2023-03-14

Spring Boot版本为1.5.6

使用的数据库是SQL Server2016,它也是在docker(windows容器)上。

问题:每当我重新启动SQL DB容器时,应用程序开始给出这个错误。

2018-10-29 16:00:08,993 ERROR pool-13-thread-1  org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
        at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
        at com.sun.proxy.$Proxy155.findOne(Unknown Source)
        at au.com.outware.swepad.tasks.WorkOrderManagerTask.taskFetchAndProcessWorkOrderDetails(WorkOrderManagerTask.java:63)
        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.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.PersistenceException: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1700)
        at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:48)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:189)
        at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
        ... 30 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:724)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.setAutoCommit(SQLServerConnection.java:2615)
        at sun.reflect.GeneratedMethodAccessor163.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
        at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79)
        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
        at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
        at com.sun.proxy.$Proxy102.setAutoCommit(Unknown Source)
        at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67)
        at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)
        at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
        at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512)
        at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45)
        ... 32 common frames omitted
2018-10-29 16:00:08,995 WARN pool-30-thread-1  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: null
2018-10-29 16:00:08,996 ERROR pool-30-thread-1  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - The connection is closed.
2018-10-29 16:30:00,017 INFO pool-13-threa
spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring:
  datasource:
    type: org.apache.tomcat.jdbc.pool.XADataSource
    tomcat:
      test-on-borrow: true
      validation-query: select 1
      validation-interval: 30000
  jpa:
    database: sql_server
    database-platform: org.hibernate.dialect.SQLServer2012Dialect
    generate-ddl: false
    hibernate:
      ddl-auto: none
---
spring:
  profiles: dev,staging,prod
  datasource:
    url: "jdbc:sqlserver://${DB_IP}:${DB_PORT};databaseName=${DB_NAME}"
    username: "${DB_USER}"
    password: "${DB_PASS}"
    initialize: false
  jpa:
    show-sql: true
    generate-ddl: false
    hibernate:
      ddl-auto: none

任何想法可以做什么来解决这个问题。

多谢了。

共有1个答案

刘安志
2023-03-14

请确保数据库在属性文件(.yaml.properties)中定义的端口IP-address上运行。

org.springframework.transaction.CannotCreateTransactionException: Could not 
open JPA EntityManager for transaction; nested exception is 
javax.persistence.PersistenceException: com.microsoft.sqlserver.jdbc.SQLServerException: 
The connection is closed.

如果您稍微仔细地看到您的异常并阅读消息,您将能够很容易地识别问题。

这里,stacktrace说EntityManager将无法初始化,因为连接已关闭。

 类似资料:
  • 根据文档,来自的room实例应该保存的数据是persist。但还是迷路了。我的项目必须数据库 第一个数据库 @database(实体=[foodmodel::class],version=4,exportSchema=false)抽象类FoodDatabase:RoomDatabase(){ 第二数据库 抽象类MyFoodDatabase:RoomDatabase(){abstract val M

  • 我们有一个Java应用程序,它使用Quartz来调度作业。我们使用的quartz版本是:Quartz-2.2.1 quartz配置使用JDBC作业存储。 如果在quartz scheduler对象上调用start方法时数据库连接关闭(由于断断续续的网络故障),它将以以下异常失败: 为了确保quartz scheduler成功启动,我们在代码中添加了重试,它在每1秒后调用quartz schedul

  • 问题内容: 我愿意在应用程序中添加一个按钮,单击该按钮将重新启动该应用程序。我搜索谷歌,但没有发现任何有用的,除了这一个。但是,此处遵循的过程违反了Java的WORA概念。 是否有其他以Java为中心的方法来实现此功能?是否可以只派生另一个副本然后退出? 提前致谢。我感谢您的帮助。 @deporter我已经尝试过您的解决方案,但是它不起作用:( @mKorbel我写的,采取的概念下面的代码,你曾在

  • 当我运行这个错误时,请帮助我解决这个问题 启动应用程序上下文时出错。要显示条件,请报告启用“调试”后重新运行应用程序。2019-02-11 10:53:55.839错误8804 --- [ restartedMain]o. s. b. d.日志失败分析记者: 应用程序启动失败 说明: com中的字段userDao。实例Spring Security应用程序编程接口。服务UserServiceImp

  • 我在stackoverflow上发现了类似的问题,并试图用这种方式(LINK)解决这个问题,但在我的项目中没有起作用。谁能给我一些建议吗? pom.xml 应用属性

  • 我有一份Java申请。 应用程序有一个决定应用程序是否在启动时启动的设置。 目前,我通过在StartUp items文件夹中放置/删除快捷方式实现了这一点。 然而,我想知道是否有更好的方法来处理这种行为。 编辑 是的,是视窗。抱歉之前没有清除。 应用程序有一个UI,用户可以在其中触发操作,并且应用程序在运行时定期在后台运行一些任务。 @Peter,如何使用应用程序中的代码更改注册表?这种方法是否与