Spring 引导:Apache derby 池为空。无法在 30 秒内获取连接

Spring Boot: Apache derby pool empty. Unable to fetch a connection in 30 seconds

运行 一段时间后,我收到此错误。
Servlet.service() for servlet [dispatcherServlet] 在路径 [] 上下文中抛出异常 [请求处理失败;嵌套异常是 org.springframework.orm.jpa.JpaSystemException:无法获取 JDBC 连接;嵌套异常是 org.hibernate.exception.GenericJDBCException: 根本原因无法获取 JDBC 连接] org.apache.tomcat.jdbc.pool.PoolExhaustedException: [http-nio-8081-exec-5] 超时:池为空。无法在 30 秒内获取连接,none 可用[size:100; busy:100; idle:0; lastwait:30000].

这是 application.properties 文件:

spring.datasource.url=jdbc:derby:/spring-boot/db;create=true
spring.datasource.username=admin
spring.datasource.password=1234
spring.datasource.driver=org.apache.derby.jdbc.EmbeddedDriver
hibernate.dialect = org.hibernate.dialect.DerbyDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

spring.datasource.max-active=100
spring.datasource.max-idle=8
spring.datasource.min-idle=8

我无法重现错误,但让我更正您的属性文件,以便 Spring 实际上可以读取您的属性,如果 您使用的是 SpringBoot 1.4 及更高版本.

正如我在 中发现的那样,为了解决您正在尝试增加 datasource.max-active 和其他属性的问题,对吗?

因此,如果属性不在 this default application properties 中,则必须将它们写为

    spring.jpa.properties.*= # Additional native properties to set on the JPA provider.

据此,我建议您将 application.properties 文件更改为以下内容:

    spring.datasource.url = jdbc:derby:/spring-boot/db;create=true
    spring.datasource.username = admin
    spring.datasource.password = 1234

    # this one is incorrect
    # spring.datasource.driver = org.apache.derby.jdbc.EmbeddedDriver
    # replace with the one below, otherwise Spring will use his own
    # derby driver
    spring.datasource.driver-class-name = org.apache.derby.jdbc.EmbeddedDriver

    # this one is incorrect as well
    # hibernate.dialect = org.hibernate.dialect.DerbyDialect
    # use the following way
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.DerbyDialect

    spring.jpa.generate-ddl = true
    spring.jpa.hibernate.ddl-auto = update
    spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext

    # and in order for these guys to work you have to know
    # spring.datasource.max-active=100
    # spring.datasource.max-idle=8
    # spring.datasource.min-idle=8
    # your dbcp and look for documentation
    # below is the example for Tomcat
    spring.datasource.tomcat.max-active=100
    spring.datasource.tomcat.max-idle=8
    spring.datasource.tomcat.min-idle=8

    # make sure you check out the logging
    # this is IMPORTANT to see that your 
    # properties were ACTUALLY picked up
    logging.level.org.springframework.web = DEBUG
    logging.level.org.hibernate = DEBUG

前3个可能帮助你克服困难的属性是最难解决的。我从this post here, last answer. It has also link to Spring Boot 1.4.1.RELEASE documentation.

中挑选了一个知识

最后但同样重要的是,您必须确保您的 max-idle 属性 确实被 Spring 选中。这就是为什么我添加了两个 最后的日志记录和调试属性。

如果您仔细查看日志文件,请检查它们设置的值。这是这条线对我的看法

    2016-11-17 11:40:27.875 DEBUG 32079 --- [           main] o.hibernate.internal.SessionFactoryImpl  : Instantiating session factory with properties: {java.vendor=Oracle Corporation, sun.java.launcher=SUN_STANDARD, catalina.base=/tmp/tomcat.736868414125414090.8080, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, catalina.useNaming=false, hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy, os.name=Linux, sun.boot.class.path=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/resources.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/rt.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/sunrsasign.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jsse.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jce.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/charsets.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jfr.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/classes, hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext, java.vm.specification.vendor=Oracle Corporation, java.runtime.version=1.8.0_73-b02, user.name=nikiforo, javax.persistence.validation.mode=AUTO, user.language=en, sun.boot.library.path=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/amd64, PID=32079, java.version=1.8.0_73, user.timezone=Europe/Berlin, sun.arch.data.model=64, java.endorsed.dirs=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/endorsed, sun.cpu.isalist=, sun.jnu.encoding=UTF-8, file.encoding.pkg=sun.io, file.separator=/, java.specification.name=Java Platform API Specification, java.class.version=52.0, user.country=US, java.home=/usr/users/nikiforo/src/jdk1.8.0_73/jre, java.vm.info=mixed mode, os.version=4.1.20-11-default, hibernate.boot.CfgXmlAccessService.key=org.hibernate.boot.cfgxml.spi.LoadedConfig@6abb7b7d, hibernate.connection.datasource=org.apache.tomcat.jdbc.pool.DataSource@5c7dfc05{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.apache.derby.jdbc.EmbeddedDriver; maxActive=100

这是一条很长的线,但正如你所见,我已经设置了 max-active

如果您想查看我在哪个简单的 Spring-Data-REST 应用程序中尝试了 derby 和以下属性,请检查 this mine GitHub project: derby-tomcat-database-error branch

可能有帮助,也可能没有帮助,如果我错了请告诉我,我会删除 post。