无法从 Wildfly 连接到 SQL Server 数据库
Posted
技术标签:
【中文标题】无法从 Wildfly 连接到 SQL Server 数据库【英文标题】:Cannot connect to SQL server database from Wildfly 【发布时间】:2021-10-04 02:56:05 【问题描述】:我正在尝试在 Wildfly 18 和 SQL Server 2017 之间建立连接。我已将驱动程序 mssql-jdbc-7.2.2.jre11.jar
添加到路径 C:\wildfly-18.0.0.Final\modules\system\layers\base\com\microsoft\sqlserver\main
。在同一个文件夹中,我添加了一个 module.xml
文件,其内容如下:
<module xmlns="urn:jboss:module:1.3" name="com.microsoft.sqlserver">
<resources>
<resource-root path="mssql-jdbc-7.2.2.jre11.jar" />
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
在C:\wildfly-18.0.0.Final\standalone\configuration\standalone.xml
文件中我有下一个:
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="$wildfly.datasources.statistics-enabled:$wildfly.statistics-enabled:false">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:/MSSQLDS" pool-name="MSSQLDS">
<connection-url>jdbc:sqlserver://127.0.0.1:1433;DatabaseName=xpresscr</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
<driver>sqlserver</driver>
<security>
<user-name>xpression</user-name>
<password>xpression</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mssql.MSSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
</validation>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="sqlserver" module="com.microsoft.sqlserver">
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
</driver>
</drivers>
</datasources>
我从 HAL 控制台添加了数据源。但是,在 HAL 控制台中测试连接时出现此错误:
"WFLYJCA0040: failed to invoke operation: WFLYJCA0047: Connection is not valid"
最后,在server.log
文件中我得到了这个异常:
2021-07-28 12:39:45,033 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 18.0.0.Final (WildFly Core 10.0.0.Final) started in 1110ms - Started 316 of 578 services (369 services are lazy, passive or on-demand)
2021-07-28 12:40:23,114 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) WFLYJCA0001: Bound data source [java:/MSSQLDS]
2021-07-28 12:40:23,255 WARN [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (External Management Request Threads -- 2) IJ000604: Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection
at org.jboss.ironjacamar.jdbcadapters@1.4.17.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:345)
at org.jboss.ironjacamar.jdbcadapters@1.4.17.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:352)
at org.jboss.ironjacamar.jdbcadapters@1.4.17.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:287)
at org.jboss.ironjacamar.impl@1.4.17.Final//org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.createConnectionEventListener(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:1328)
at org.jboss.ironjacamar.impl@1.4.17.Final//org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:499)
at org.jboss.ironjacamar.impl@1.4.17.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.internalTestConnection(AbstractPool.java:1067)
at org.jboss.ironjacamar.impl@1.4.17.Final//org.jboss.jca.core.connectionmanager.pool.strategy.OnePool.testConnection(OnePool.java:93)
at org.jboss.as.connector@18.0.0.Final//org.jboss.as.connector.subsystems.common.pool.PoolOperations$TestConnectionInPool.invokeCommandOn(PoolOperations.java:240)
at org.jboss.as.connector@18.0.0.Final//org.jboss.as.connector.subsystems.common.pool.PoolOperations$1.execute(PoolOperations.java:97)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.AbstractOperationContext.executeStep(AbstractOperationContext.java:999)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:743)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:467)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.OperationContextImpl.executeOperation(OperationContextImpl.java:1413)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.ModelControllerImpl.internalExecute(ModelControllerImpl.java:423)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.ModelControllerImpl.lambda$execute$1(ModelControllerImpl.java:243)
at org.wildfly.security.elytron-private@1.10.3.Final//org.wildfly.security.auth.server.SecurityIdentity.runAs(SecurityIdentity.java:289)
at org.wildfly.security.elytron-private@1.10.3.Final//org.wildfly.security.auth.server.SecurityIdentity.runAs(SecurityIdentity.java:255)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.ModelControllerImpl.execute(ModelControllerImpl.java:243)
at org.jboss.as.domain-http-interface@10.0.0.Final//org.jboss.as.domain.http.server.DomainApiHandler.handleRequest(DomainApiHandler.java:212)
at io.undertow.core@2.0.26.Final//io.undertow.server.handlers.encoding.EncodingHandler.handleRequest(EncodingHandler.java:72)
at org.jboss.as.domain-http-interface@10.0.0.Final//org.jboss.as.domain.http.server.DomainApiCheckHandler.handleRequest(DomainApiCheckHandler.java:93)
at org.jboss.as.domain-http-interface@10.0.0.Final//org.jboss.as.domain.http.server.security.ElytronIdentityHandler.lambda$handleRequest$0(ElytronIdentityHandler.java:62)
at org.wildfly.security.elytron-private@1.10.3.Final//org.wildfly.security.auth.server.SecurityIdentity.runAs(SecurityIdentity.java:313)
at org.wildfly.security.elytron-private@1.10.3.Final//org.wildfly.security.auth.server.SecurityIdentity.runAs(SecurityIdentity.java:270)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.AccessAuditContext.doAs(AccessAuditContext.java:254)
at org.jboss.as.controller@10.0.0.Final//org.jboss.as.controller.AccessAuditContext.doAs(AccessAuditContext.java:225)
at org.jboss.as.domain-http-interface@10.0.0.Final//org.jboss.as.domain.http.server.security.ElytronIdentityHandler.handleRequest(ElytronIdentityHandler.java:61)
at io.undertow.core@2.0.26.Final//io.undertow.server.handlers.BlockingHandler.handleRequest(BlockingHandler.java:56)
at io.undertow.core@2.0.26.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
at io.undertow.core@2.0.26.Final//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
at org.jboss.threads@2.3.3.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database "xpresscr" requested by the login. The login failed. ClientConnectionId:53217f16-f5a2-46d9-b596-13a9d01b1691
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:258)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:104)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:5036)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3668)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3627)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2935)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2456)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2103)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1950)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1162)
at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:735)
at org.jboss.ironjacamar.jdbcadapters@1.4.17.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:321)
... 35 more
2021-07-28 12:40:23,271 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 2) WFLYCTL0013: Operation ("test-connection-in-pool") failed - address: ([
("subsystem" => "datasources"),
("data-source" => "MSSQLDS")
]) - failure description: "WFLYJCA0040: failed to invoke operation: WFLYJCA0047: Connection is not valid"
从 MS SQL Server Management Studio,我可以使用 xpression
用户连接到 xpresscr
数据库。我不知道为什么我无法从 Wildfly 连接。有什么帮助吗?
谢谢!!
【问题讨论】:
无法打开登录请求的数据库“xpresscr”。登录失败。 您的计算机上是否安装了多个版本/实例的 SQL Server?127.0.0.1:1433
将尝试连接到“默认”实例,通常称为 MSSQLSERVER
。
嗨@AlwaysLearning,感谢您的回复。这是一台新的 Windows 机器。我刚刚安装了 SQL Server 2017。我只创建了一个数据库,添加了一个用户并更改了一些设置。
在 SQL Server [2017] 配置管理器 > SQL Server 网络配置 > MSSQLSERVER 的协议中是否启用了 TCP/IP 协议?是否配置为侦听 127.0.0.1 端口 1433?注意:如果您在此处更改设置,则需要重新启动 SQL Server 服务才能使更改生效。
嗨@AlwaysLearning,TCP/IP 协议已启用。在其属性中,在“IP 地址”选项卡中,我有四个“IP + 号码”和一个“IPAll”。 “IP4”的 IP 地址为 127.0.0.1。其余的“IP+号码”有不同的IP地址。所有这些“IP + 号码”设置都处于活动状态,但未启用。
以前没有注意到这一点,SQLServerException: 无法打开登录请求的数据库“xpresscr”。登录失败。这表明它能够建立 TCP 连接,但是还没有发生一些安全配置/用户映射。尝试在 SQL Server ERRORLOG 文件中查看登录失败事件 18456,然后将状态编号与 MSSQLSERVER_18456 进行比较,以了解 SQL Server 认为问题所在。
【参考方案1】:
-
确保您的 SQL SERVER 正在侦听 1433 端口:
2。如果是这样,您的连接字符串可能无效,请尝试
以下格式的连接字符串:
sqlserver://username:password@host:port?database=dbname
即sqlserver://sa:123@127.0.0.1:1433?database=xpresscr
【讨论】:
以上是关于无法从 Wildfly 连接到 SQL Server 数据库的主要内容,如果未能解决你的问题,请参考以下文章