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

如何配置EclipseLink以使用HikariCP作为其连接池

董联
2023-03-14

我使用EclipseLink作为使用Play框架和Akka的应用程序的持久性提供程序。在play framework 2.3.x中,应用程序正常运行。现在我正在迁移到2.4.x,随后将迁移到2.5.x,但是当我从eclipse外部运行应用程序时遇到了一些问题:

“没有名为Default的EntityManager的持久性提供程序”。

随着Play Framework连接池库从BoneCP更改为HikariCP,我认为我可能也应该将该连接池与EclipseLink一起使用。

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

  <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <!-- If <non-jta-data-source> is specified, the DB settings are read from application.conf (db.*.jndiName and db.default.*) -->
    <!-- Also, <non-jta-data-source> is working fine with 'activator run' but causing strange exceptions when unloading the WAR in Jetty 9.2.3. -->
    <non-jta-data-source>DefaultDS</non-jta-data-source>

    <!-- Eclipse link needs the classes to be properly declared here whereas hibernate (seemingly) does not. 
    Keep up to date by searching for @Entity and adding all the found classes properly and add the converters. -->
    <class>acm.data.Authorization</class>     
    <class>acm.data.LocalUser</class>
    <class>acm.data.ResourceAssignedRole</class>
    <class>acm.data.UserAssignedRole</class>
    <class>acm.data.UserRole</class>

    <class>alloc.data.MacroAllocation</class>
    <class>alloc.data.MicroAllocation</class>

    <class>clf.data.ClientStore</class>

    <class>fco.data.Costs</class>
    <class>fco.data.CostsSet</class>
    <class>fco.data.CostsType</class>

    <class>platform.data.DurationConverter</class>
    <class>platform.data.EnumType</class>
    <class>platform.data.EnumValue</class>
    <class>platform.data.Log</class>
    <class>platform.data.LogSet</class>
    <class>platform.data.Property</class>
    <class>platform.data.PropertySet</class>
    <class>platform.data.IntlStringConverter</class>
    <class>platform.data.StringLocalDateTimeConverter</class>
    <class>platform.data.LocalDateConverter</class>
    <class>platform.data.StringLocalDateConverter</class>
    <class>platform.data.LocalDateTimeConverter</class>
    <class>platform.license.License</class>

    <class>pm.data.Indicator</class>
    <class>pm.data.IndicatorSet</class>
    <class>pm.data.Phase</class>
    <class>pm.data.PhaseDependency</class>
    <class>pm.data.PhaseRequirement</class>
    <class>pm.data.Project</class>
    <class>pm.data.ProjectGuideline</class>
    <class>pm.data.ProjectMember</class>
    <class>pm.data.ProjectReport</class>
    <class>pm.data.Request</class>
    <class>pm.data.RequestSet</class>
    <class>pm.data.Risk</class>
    <class>pm.data.RiskSet</class>
    <class>pm.data.Role</class>
    <class>pm.data.Scenario</class>
    <class>pm.data.StatusAndForecast</class>
    <class>pm.data.StatusAndForecastSet</class>

    <class>resource.data.Absence</class>
    <class>resource.data.Availability</class>
    <class>resource.data.Holiday</class>
    <class>resource.data.HolidayLocale</class>
    <class>resource.data.Resource</class>
    <class>resource.data.ResourceGroup</class>
    <class>resource.data.ResourceLink</class>
    <class>resource.data.ResourceType</class>
    <class>resource.data.ResourceTypeLink</class>
    <class>platform.data.EclipseLinkSessionCustomizer</class>
    <!-- <exclude-unlisted-classes>false</exclude-unlisted-classes>--> <!-- Include all classes -->
    <properties>
      <!-- MSSQL -->
      <!--
      <property name="javax.persistence.jdbc.driver"          value="net.sourceforge.jtds.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url"             value="jdbc:jtds:sqlserver://SVG6030-DBTE-01/projectx_dev;instance=MSSQL2008R2"/>
      <property name="javax.persistence.jdbc.user"            value="projectx_dev"/>
      <property name="javax.persistence.jdbc.password"        value="projectx_pass"/>
       -->
      <!-- Configuring Eclipse Link -->
      <property name="eclipselink.logging.level" value="OFF"/> <!-- How much log should be shown | from: https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging#Log_Level_Configuration-->
      <property name="eclipselink.logging.level.sql" value="OFF"/> <!-- How to show the sql queries -->
      <property name="eclipselink.target-database" value="SQLServer"/> <!-- What sql database is used | from: http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_target_database.htm-->
      <property name="eclipselink.ddl-generation" value="none"/>
      <property name="eclipselink.session.customizer" value="platform.data.EclipseLinkSessionCustomizer"/> <!-- Defines a naming strategy  -->

      <!-- Connection pooling for productive systems --> <!-- http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_connection_pool.htm -->
      <property name="eclipselink.connection-pool.default.initial" value="50" />
      <property name="eclipselink.connection-pool.node2.min" value="5"/>
      <property name="eclipselink.connection-pool.node2.max" value="100"/>
      <!-- TODO: Add c3p0 to eclipse link: 
      http://stackoverflow.com/questions/17828377/eclipselink-pooling-equivalent-to-c3po
      http://www.eclipse.org/forums/index.php/t/172073/ -->

    </properties>
  </persistence-unit>    
</persistence> 

application.conf

# This is the main configuration file for the application.
# https://www.playframework.com/documentation/latest/ConfigFile
# ~~~~~
# Play uses HOCON as its configuration file format.  HOCON has a number
# of advantages over other config formats, but there are two things that
# can be used when modifying settings.
#
# You can include other configuration files in this main application.conf file:
#include "extra-config.conf"
#
# You can declare variables and substitute for them:
#mykey = ${some.value}
#
# And if an environment variable exists when there is no other subsitution, then
# HOCON will fall back to substituting environment variable:
#mykey = ${JAVA_HOME}


## Secret key
# http://www.playframework.com/documentation/latest/ApplicationSecret
# ~~~~~
# The secret key is used to sign Play's session cookie.
# This must be changed for production, but we don't recommend you change it in this file.
play.crypto.secret="xxx"

## Modules
# https://www.playframework.com/documentation/latest/Modules
# ~~~~~
# Control which modules are loaded when Play starts.  Note that modules are
# the replacement for "GlobalSettings", which are deprecated in 2.5.x.
# Please see https://www.playframework.com/documentation/latest/GlobalSettings
# for more information.
#
# You can also extend Play functionality by using one of the publically available
# Play modules: https://playframework.com/documentation/latest/ModuleDirectory
play.modules {
  # By default, Play will load any class called Module that is defined
  # in the root package (the "app" directory), or you can define them
  # explicitly below.
  # If there are any built-in modules that you want to disable, you can list them here.
  #enabled += my.application.Module

  # If there are any built-in modules that you want to disable, you can list them here.
  #disabled += ""
}

## IDE
# https://www.playframework.com/documentation/latest/IDE
# ~~~~~
# Depending on your IDE, you can add a hyperlink for errors that will jump you
# directly to the code location in the IDE in dev mode. The following line makes 
# use of the IntelliJ IDEA REST interface: 
#play.editor="http://localhost:63342/api/file/?file=%s&line=%s"

## Internationalisation
# https://www.playframework.com/documentation/latest/JavaI18N
# https://www.playframework.com/documentation/latest/ScalaI18N
# ~~~~~
# Play comes with its own i18n settings, which allow the user's preferred language
# to map through to internal messages, or allow the language to be stored in a cookie.
play.i18n {
  # The application languages
  langs = [ "en" ]

  # Whether the language cookie should be secure or not
  #langCookieSecure = true

  # Whether the HTTP only attribute of the cookie should be set to true
  #langCookieHttpOnly = true
}

## Play HTTP settings
# ~~~~~
play.http {
  ## Router
  # https://www.playframework.com/documentation/latest/JavaRouting
  # https://www.playframework.com/documentation/latest/ScalaRouting
  # ~~~~~
  # Define the Router object to use for this application.
  # This router will be looked up first when the application is starting up,
  # so make sure this is the entry point.
  # Furthermore, it's assumed your route file is named properly.
  # So for an application router like `my.application.Router`,
  # you may need to define a router file `conf/my.application.routes`.
  # Default to Routes in the root package (aka "apps" folder) (and conf/routes)
  #router = my.application.Router

  ## Action Creator
  # https://www.playframework.com/documentation/latest/JavaActionCreator
  # ~~~~~
  #actionCreator = null

  ## ErrorHandler
  # https://www.playframework.com/documentation/latest/JavaRouting
  # https://www.playframework.com/documentation/latest/ScalaRouting
  # ~~~~~
  # If null, will attempt to load a class called ErrorHandler in the root package,
  #errorHandler = null

  ## Filters
  # https://www.playframework.com/documentation/latest/ScalaHttpFilters
  # https://www.playframework.com/documentation/latest/JavaHttpFilters
  # ~~~~~
  # Filters run code on every request. They can be used to perform
  # common logic for all your actions, e.g. adding common headers.
  # Defaults to "Filters" in the root package (aka "apps" folder)
  # Alternatively you can explicitly register a class here.
  #filters = my.application.Filters

  ## Session & Flash
  # https://www.playframework.com/documentation/latest/JavaSessionFlash
  # https://www.playframework.com/documentation/latest/ScalaSessionFlash
  # ~~~~~
  session {
    # Sets the cookie to be sent only over HTTPS.
    #secure = true

    # Sets the cookie to be accessed only by the server.
    #httpOnly = true

    # Sets the max-age field of the cookie to 5 minutes.
    # NOTE: this only sets when the browser will discard the cookie. Play will consider any
    # cookie value with a valid signature to be a valid session forever. To implement a server side session timeout,
    # you need to put a timestamp in the session and check it at regular intervals to possibly expire it.
    #maxAge = 300

    # Sets the domain on the session cookie.
    #domain = "example.com"
  }

  flash {
    # Sets the cookie to be sent only over HTTPS.
    #secure = true

    # Sets the cookie to be accessed only by the server.
    #httpOnly = true
  }

  parser {
     maxMemoryBuffer = 1048576
  }

  requestHandler = "play.http.DefaultHttpRequestHandler"
}

## Netty Provider
# https://www.playframework.com/documentation/latest/SettingsNetty
# ~~~~~
play.server.netty {
  # Whether the Netty wire should be logged
  #log.wire = true

  # If you run Play on Linux, you can use Netty's native socket transport
  # for higher performance with less garbage.
  #transport = "native"
}

## WS (HTTP Client)
# https://www.playframework.com/documentation/latest/ScalaWS#Configuring-WS
# ~~~~~
# The HTTP client primarily used for REST APIs.  The default client can be
# configured directly, but you can also create different client instances
# with customized settings. You must enable this by adding to build.sbt:
#
# libraryDependencies += ws // or javaWs if using java
#
play.ws {
  # Sets HTTP requests not to follow 302 requests
  #followRedirects = false

  # Sets the maximum number of open HTTP connections for the client.
  #ahc.maxConnectionsTotal = 50

  ## WS SSL
  # https://www.playframework.com/documentation/latest/WsSSL
  # ~~~~~
  ssl {
    # Configuring HTTPS with Play WS does not require programming.  You can
    # set up both trustManager and keyManager for mutual authentication, and
    # turn on JSSE debugging in development with a reload.
    #debug.handshake = true
    #trustManager = {
    #  stores = [
    #    { type = "JKS", path = "exampletrust.jks" }
    #  ]
    #}
  }
}

## Cache
# https://www.playframework.com/documentation/latest/JavaCache
# https://www.playframework.com/documentation/latest/ScalaCache
# ~~~~~
# Play comes with an integrated cache API that can reduce the operational
# overhead of repeated requests. You must enable this by adding to build.sbt:
#
# libraryDependencies += cache
#
play.cache {
  # If you want to bind several caches, you can bind the individually
  #bindCaches = ["db-cache", "user-cache", "session-cache"]
}

## Filters
# https://www.playframework.com/documentation/latest/Filters
# ~~~~~
# There are a number of built-in filters that can be enabled and configured
# to give Play greater security.  You must enable this by adding to build.sbt:
#
# libraryDependencies += filters
#
play.filters {
  ## CORS filter configuration
  # https://www.playframework.com/documentation/latest/CorsFilter
  # ~~~~~
  # CORS is a protocol that allows web applications to make requests from the browser
  # across different domains.
  # NOTE: You MUST apply the CORS configuration before the CSRF filter, as CSRF has
  # dependencies on CORS settings.
  cors {
    # Filter paths by a whitelist of path prefixes
    #pathPrefixes = ["/some/path", ...]

    # The allowed origins. If null, all origins are allowed.
    #allowedOrigins = ["http://www.example.com"]

    # The allowed HTTP methods. If null, all methods are allowed
    #allowedHttpMethods = ["GET", "POST"]
  }

  ## CSRF Filter
  # https://www.playframework.com/documentation/latest/ScalaCsrf#Applying-a-global-CSRF-filter
  # https://www.playframework.com/documentation/latest/JavaCsrf#Applying-a-global-CSRF-filter
  # ~~~~~
  # Play supports multiple methods for verifying that a request is not a CSRF request.
  # The primary mechanism is a CSRF token. This token gets placed either in the query string
  # or body of every form submitted, and also gets placed in the users session.
  # Play then verifies that both tokens are present and match.
  csrf {
    # Sets the cookie to be sent only over HTTPS
    #cookie.secure = true

    # Defaults to CSRFErrorHandler in the root package.
    #errorHandler = MyCSRFErrorHandler
  }

  ## Security headers filter configuration
  # https://www.playframework.com/documentation/latest/SecurityHeaders
  # ~~~~~
  # Defines security headers that prevent XSS attacks.
  # If enabled, then all options are set to the below configuration by default:
  headers {
    # The X-Frame-Options header. If null, the header is not set.
    #frameOptions = "DENY"

    # The X-XSS-Protection header. If null, the header is not set.
    #xssProtection = "1; mode=block"

    # The X-Content-Type-Options header. If null, the header is not set.
    #contentTypeOptions = "nosniff"

    # The X-Permitted-Cross-Domain-Policies header. If null, the header is not set.
    #permittedCrossDomainPolicies = "master-only"

    # The Content-Security-Policy header. If null, the header is not set.
    #contentSecurityPolicy = "default-src 'self'"
  }

  ## Allowed hosts filter configuration
  # https://www.playframework.com/documentation/latest/AllowedHostsFilter
  # ~~~~~
  # Play provides a filter that lets you configure which hosts can access your application.
  # This is useful to prevent cache poisoning attacks.
  hosts {
    # Allow requests to example.com, its subdomains, and localhost:9000.
    #allowed = [".example.com", "localhost:9000"]
  }
}

## Evolutions
# https://www.playframework.com/documentation/latest/Evolutions
# ~~~~~
# Evolutions allows database scripts to be automatically run on startup in dev mode
# for database migrations. You must enable this by adding to build.sbt:
#
# libraryDependencies += evolutions
#
play.evolutions {
  # You can disable evolutions for a specific datasource if necessary
  #db.default.enabled = false
}

## Database Connection Pool
# https://www.playframework.com/documentation/latest/SettingsJDBC
# ~~~~~
# Play doesn't require a JDBC database to run, but you can easily enable one.
#
#libraryDependencies += jdbc
#
play.db {
  # The combination of these two settings results in "db.default" as the
  # default JDBC pool:
  config = "db"
  default = "default"

  # Play uses HikariCP as the default connection pool.  You can override
  # settings by changing the prototype:
  prototype {
    # hikaricp.dataSourceClassName = "default"
    # specify a connectionTestQuery. Only do this if upgrading the JDBC-Driver does not help
    hikaricp.connectionTestQuery = "SELECT 1" # a bug in the driver when used with hikaricp in Play 2.4, workaround from here: https://www.playframework.com/documentation/2.5.x/Migration24
    hikaricp.minimumIdle = 5
    hikaricp.maximumPoolSize = 100
  }
}

## JDBC Datasource
# https://www.playframework.com/documentation/latest/JavaDatabase
# https://www.playframework.com/documentation/latest/ScalaDatabase
# ~~~~~
# Once JDBC datasource is set up, you can work with several different
# database options:
#
# Slick (Scala preferred option): https://www.playframework.com/documentation/latest/PlaySlick
# JPA (Java preferred option): https://playframework.com/documentation/latest/JavaJPA
# EBean: https://playframework.com/documentation/latest/JavaEbean
# Anorm: https://www.playframework.com/documentation/latest/ScalaAnorm
#
db {

  # You can declare as many datasources as you want.
  # By convention, the default datasource is named `default`

  # Microsoft SQL Server (using free/open jTDS driver)
  # The following is working fine with 'activator run' but causing strange exceptions when unloading the WAR in Jetty 9.2.3.

  default.jndiName=DefaultDS

  # Microsoft SQL Server (using free/open jTDS driver)
  # The following is working fine with 'activator run' but causing strange exceptions when unloading the WAR in Jetty 9.2.3.

  # Huenenberg
  default.driver=net.sourceforge.jtds.jdbc.Driver
  default.url="jdbc:jtds:sqlserver://xxx/xxx_dev;instance=MSSQL2008R2"
  default.username=xxx_dev
  default.password=xxx_pass


  # https://www.playframework.com/documentation/latest/Developing-with-the-H2-Database
  #default.driver = org.h2.Driver
  #default.url = "jdbc:h2:mem:play"
  #default.username = sa
  #default.password = ""

  # You can turn on SQL logging for any datasource
  # https://www.playframework.com/documentation/latest/Highlights25#Logging-SQL-statements
  #default.logSql=true
}

# Akka configuration 
# ==================

akka {
  # Let Akka use the same logging as Play
  loggers = ["akka.event.slf4j.Slf4jLogger"]
  loglevel = "DEBUG"
  #  This will filter the log events using the backend configuration (e.g. logback.xml) before they are published to the event bus.
  logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"

  actor {
    # Timeout for ask(), the max. acceptable value seems to be 30sec anyway.
    typed.timeout=3600000  

    # Verify that messages are serializable, only for running tests and to ensure remoting is possible. 
    # It is completely pointless to have it turned on in other scenarios.
    # serialize-messages = on

    # Increase timeouts for running tests, the max. timeout seems to be 30sec.
    # test.timefactor=100

    default-dispatcher { // Akka default config for Play 2.4 http://doc.akka.io/docs/akka/2.4.9/general/configuration.html#listing-of-the-reference-configuration
      fork-join-executor {
        parallelism-factor = 3.0
        parallelism-max = 64
        task-peeking-mode = FIFO
      }
    }
  } 
}

# Configuration for sending emails
smtp {
    host = "xxxx" 
    port = 25 
    debug = false 
    mock = false
}

# Information about this xxx instance
pqforce {
    url = "http://localhost:9000/"
    email = "xxx"
}

# Inactive clustering configuration, see clustering.conf for an alternative active configuration.
projectx {
  remotes = [ ]
  accesscontrol.provider      = "acm.business.LocalAccessControlServiceImpl",
  accesscontrol.timeout       = 300000,
  accesscontrol.cachesize     = 1000,
  accesscontrol.sessionexpiry = 1200
}

共有1个答案

傅玮
2023-03-14

这个问题很难解决。显然,唯一重要的是包含带有persistence.xml的META-INF的conf文件夹不在类路径上,因此我必须在从Eclipse外部启动应用程序时添加它。

在我的例子中,这意味着我以以下内容启动应用程序:

java -cp .;target/universal/stage/lib/*;target/universal/stage/conf/

而后一个路径是通向META-INF文件夹的路径。

 类似资料:
  • 我试图在postgresql中使用HikariCP,但我在任何地方都找不到postgresql的配置。 请给我指出任何示例postgresql与HikariCP或任何配置教程相同。 我试着像下面那样使用它,但它不起作用,然后我意识到它是为MySQL设计的

  • 问题内容: 我有一个问题,如果mySQL Server在“睡眠时间” 500秒后终止了会话,则下一个请求不会成功。如果mySQL Server没有关闭睡眠连接,则可能在700秒后发生相同的问题。 我能做什么?遵循我的persistence.xml的属性 如果重要的话,transactiontype为RESOURCE_LOCAL。 问题答案: 您如何配置连接池?如果它是服务器数据源,则应在服务器中设

  • 我的WebApp使用多个数据库,我尝试使用GlassFish连接池来管理连接,但我发现配置示例只使用一个数据库。 那么,我该怎么办?创建与我正在使用的数据库数量相同的连接池,或者是否有方法将一个池配置为多个数据库?

  • 我想在应用程序和Oracle数据库之间建立连接。我没有以下数据库信息: URL 用户名 密码 我使用的配置如下所示: 这就是我如何获得一个新的实例: 之后,我尝试通过以下代码检索会话: null 但是,使用以下堆栈跟踪生成错误:

  • 我有一个Spring Boot(v2.0.8)应用程序,它使用HikariCP(v2.7.9)池(连接到MariaDB)配置: 问题在于,我们的生产组件每隔几周就会反复抛出 。问题在于它永远不会从中恢复,并且会不断引发异常。因此,需要重新启动计算装置。 从HikariPool源代码来看,这似乎正在发生,因为每次它调用poolEntry都是空的,因此会抛出超时异常。要使其为空,连接池必须没有空闲条目