问题
@DataSourceDefinition
提供url
属性,但将被忽略。如果 url 设置为,将建立到jdbc:postgres://database:5432/demo
的连接。通过将它们设置为,和直接localhost:5432
设置,相同的设置效果很好。serverName
portNumber
databaseName
演示
可以在https://github.com/ThomasPr/DataSourceDefinition找到一个说明此行为的小型演示项目
细节
工作示例✅</h3>
属性serverName
,portNumber
和databaseName
是单独设置的。database:5432
将成功建立与 postgres 的连接。
@DataSourceDefinition(
name = "java:global/jdbc/DemoDataSource",
className = "org.postgresql.ds.PGSimpleDataSource",
serverName = "database", // set the property
portNumber = 5432, // set the property
databaseName = "demo", // set the property
user = "demo",
password = "demo")
失败的例子❌</h3>
将再次使用工作示例中的相同设置,但这次只设置url
属性。结果是连接尝试localhost:5432
而不是database:5432
@DataSourceDefinition(
name = "java:global/jdbc/DemoDataSource",
className = "org.postgresql.ds.PGSimpleDataSource",
url = "jdbc:postgresql://database:5432/demo", // only use url, but no serverName, portNumber or databaseName
user = "demo",
password = "demo")
奇怪的例子
与失败示例中一样使用相同的 url 设置。但是serverName
,portNumber
和databaseName
被设置为空值。很奇怪:这一次连接database:5432
成功了。与失败示例的唯一区别是提到的三个空值。
@DataSourceDefinition(
name = "java:global/jdbc/DemoDataSource",
className = "org.postgresql.ds.PGSimpleDataSource",
url = "jdbc:postgresql://database:5432/demo", // use the url to configure connection properties
user = "demo",
password = "demo",
serverName = "", // set it to an empty value
portNumber = -1, // set it to an empty value
databaseName = "") // set it to an empty value
例外
在Payara启动过程中,失败的情况下会出现如下异常:
microservice_1 | [2019-12-13T14:29:00.071+0000] [] [SEVERE] [] [org.eclipse.persistence.session./file:/tmp/payaramicro-rt7729847129244438567tmp/applications/ROOT/WEB-INF/classes/_demo.ejb] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1576247340071] [levelValue: 1000] [[
microservice_1 |
microservice_1 | Local Exception Stack:
microservice_1 | Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.4.payara-p2): org.eclipse.persistence.exceptions.DatabaseException
microservice_1 | Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
microservice_1 | Error Code: 0
microservice_1 | at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:318)
microservice_1 | at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:150)
microservice_1 | at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:172)
microservice_1 | at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:233)
microservice_1 | at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:815)
...