Wildfly 将 mysql 注册为数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24990224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wildfly registering mysql as a datasource
提问by trooper31
I have been trying to configure mysql as a datasource in wildfly. I am not sure what i am missing out, i get an error on startup .
我一直在尝试将 mysql 配置为 Wildfly 中的数据源。我不确定我错过了什么,我在启动时出错。
I have the mysql-connector-java-5.0.8-bin.jar and the module.xml in the folder: "/wildfly-8.1.0.Final/modules/system/layers/base/com/mysql/main"
我在文件夹中有 mysql-connector-java-5.0.8-bin.jar 和 module.xml:“/wildfly-8.1.0.Final/modules/system/layers/base/com/mysql/main”
below are the files
下面是文件
module.xml
模块.xml
<module xmlns="urn:jboss:module:1.1"
name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.0.8.jar"/>
<!-- Insert resources here -->
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
standalone.xml
独立文件
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<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 jta="true" jndi-name="java:jboss/datasources/proj" pool-name="proj" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>admin123</password>
</security>
<statement>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
I have tested the jdbc connectivity with a standalone program in eclipse and it worked
我已经在 Eclipse 中使用独立程序测试了 jdbc 连接,并且它工作正常
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectSql {
public static void main(String []args){
String userName = "root";
String pass = "admin123";
String url = "jdbc:mysql://localhost/";
String driver ="com.mysql.jdbc.Driver";
String db = "proj";
try{
//registering the driver.
Class.forName(driver);
Connection con = DriverManager.getConnection(url+db,userName,pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select f_name from t_users");
while(rs.next()){
System.out.println("name :"+rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(ClassNotFoundException cnf){
cnf.printStackTrace();
}
}
}
Referred to the below link and modified the module.xml
参考下面的链接并修改了module.xml
Wildfly 8.0.0 mysql problems with datasource
Here is a the error log i get on start up
这是我在启动时得到的错误日志
02:45:17,169 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("add") failed - address: ([ ("subsystem" => "datasources"), ("data-source" => "proj") ]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => [ "jboss.data-source.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]", "jboss.driver-demander.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]" ]} 02:45:17,175 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("add") failed - address: ([ ("subsystem" => "datasources"), ("data-source" => "proj") ]) - failure description: { "JBAS014771: Services with missing/unavailable dependencies" => [ "jboss.data-source.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]", "jboss.driver-demander.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]" ], "JBAS014879: One or more services were unable to start due to one or more indirect dependencies not being available." => { "Services that were unable to start:" => [ "jboss.data-source.reference-factory.proj", "jboss.naming.context.java.jboss.datasources.proj" ], "Services that may be the cause:" => ["jboss.jdbc-driver.mysql"] } }
) ]) - 失败描述:{ "JBAS014771: Services with missing/unavailable dependencies" => [ "jboss.data-source.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]", "jboss .driver-demander.java:jboss/datasources/proj is missing [jboss.jdbc-driver.mysql]" ], "JBAS014879: 由于一个或多个间接依赖项不可用,一个或多个服务无法启动。=> { "无法启动的服务:" => [ "jboss.data-source.reference-factory.proj", "jboss.naming.context.java.jboss.datasources.proj" ], "可能的服务是原因:" => ["jboss.jdbc-driver.mysql"] } } ], "JBAS014879:由于一个或多个间接依赖项不可用,一个或多个服务无法启动。" => { "无法启动的服务:" => [ "jboss.data-source.reference-factory.proj", "jboss.naming.context.java.jboss.datasources.proj" ], "可能的服务是原因:" => ["jboss.jdbc-driver.mysql"] } } ], "JBAS014879:由于一个或多个间接依赖项不可用,一个或多个服务无法启动。" => { "无法启动的服务:" => [ "jboss.data-source.reference-factory.proj", "jboss.naming.context.java.jboss.datasources.proj" ], "可能的服务是原因:" => ["jboss.jdbc-driver.mysql"] } }
回答by trooper31
To resolve the mysql datasource configuration issue on Wildfly i used the admin console to add the datasource and test it.
为了解决 Wildfly 上的 mysql 数据源配置问题,我使用管理控制台添加数据源并对其进行测试。
If you login to the web console and find the datasource you tried configuring disable that a remove it . The standalone.xml and the module.xml get reset to the orginal.
Configure a new datasource :
name :mysql
JNDI :java:jboss/datasources/projClick next and enter the url (i used the below) and click enable and then click test
url : jdbc:mysql://localhost/projThe test should show success
The changes to the standalone.xml and the module.xml are automatically made .
If you restart the server now it should start without any errors and you should be able to access the database from your web project
如果您登录到 Web 控制台并找到您尝试配置的数据源,请禁用删除它。Standalone.xml 和 module.xml 被重置为原始文件。
配置一个新的数据源:
名称 :mysql
JNDI :java:jboss/datasources/proj单击下一步并输入 url(我使用下面的)并单击启用,然后单击测试
url:jdbc:mysql://localhost/proj测试应该显示成功
对standalone.xml 和module.xml 的更改是自动进行的。
如果您现在重新启动服务器,它应该不会出现任何错误,并且您应该能够从您的 Web 项目访问数据库
回答by NiematojakTomasz
I have working configuration of mysql driver on wildfly.
我在wildfly 上有mysql 驱动程序的工作配置。
<datasources>
// ...
<drivers>
// ...
<driver name="mysql" module="com.mysql.jdbc">
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<driver-class>com.mysql.jdbc.Driver</driver-class>
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
The little difference seems to be presence of "driver-class" tag.
小小的区别似乎是“驱动程序级”标签的存在。
I hope it will help.
我希望它会有所帮助。
(Deploying mysql driver as a deployment also works and it's recommended way. https://docs.jboss.org/author/display/WFLY8/DataSource+configuration)
(将 mysql 驱动程序部署为部署也有效,这是推荐的方式。https://docs.jboss.org/author/display/WFLY8/DataSource+configuration)
[edit]
[编辑]
I have mysql module under wildfly/modules/com/mysql/jdbc/main/ . I've noticed just now you module path doesn't correspond to module name. And I haven't find any doc's just yet, but I don't think you should mess with modules/system directory.
我在 wildfly/modules/com/mysql/jdbc/main/ 下有 mysql 模块。我刚刚注意到您的模块路径与模块名称不对应。而且我还没有找到任何文档,但我认为您不应该弄乱模块/系统目录。
module.xml
模块.xml
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql.jdbc">
<resources>
<resource-root path="mysql-connector-java-5.1.31-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
回答by Michael Guinn
Two things: I just got mine working, and have the driver and xml located in $JBOSS_HOME/modules/com/mysql/jdbc/main - I'm fairly new with this as well so I'm not sure if that matters. Another thing: I don't know if it is just a typo, but if it was just copied and pasted it looks like your JAR name is incorrect
两件事:我刚刚开始工作,并且驱动程序和 xml 位于 $JBOSS_HOME/modules/com/mysql/jdbc/main - 我对此也很陌生,所以我不确定这是否重要。另一件事:我不知道这是否只是一个错字,但如果它只是复制粘贴,看起来您的 JAR 名称不正确
<resource-root path="mysql-connector-java-5.0.8.jar"/>
when it should be
什么时候应该
<resource-root path="mysql-connector-java-5.0.8-bin.jar"/>
I forgot the .jar in mine, and that ended up fixing it. Hope that helps!
我忘记了我的 .jar 文件,最终修复了它。希望有帮助!
回答by ACV
This may happen when you download the mysql connector zip and uploading the zip as a deployment. But the correct way is to unzip your download and point to the contained jar.
当您下载 mysql 连接器 zip 并将 zip 作为部署上传时,可能会发生这种情况。但正确的方法是解压缩您的下载并指向包含的 jar。
回答by PVR
There are three waysusing which you can simply create datasource into wildfly
有三种方法使用,你可以简单地创建数据源到wildfly
- Using admin console
- Manually adding into standalone.xml
- Creating datasource file that is xml file.
- 使用管理控制台
- 手动添加到 standalone.xml
- 创建数据源文件,即 xml 文件。
Go to WildFly directory/standalone/deploymentsSimplest way to create datasource xml with following content
转到WildFly 目录/standalone/deployments使用以下内容创建数据源 xml 的最简单方法
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
<datasource jndi-name="APP_DS" pool-name="APP_DS" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://localhost:3306/DB_NAME</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<!-- sql to call when connection is created -->
<new-connection-sql>SELECT 1</new-connection-sql>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>50</max-pool-size>
</pool>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
<!-- sql to call on an existing pooled connection when it is obtained from pool -->
<validation>
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
</validation>
<timeout>
<blocking-timeout-millis>300000</blocking-timeout-millis>
<idle-timeout-minutes>5</idle-timeout-minutes>
</timeout>
<statement>
<track-statements>true</track-statements>
</statement>
</datasource>
</datasources>