Wildfly将mysql注册为数据源 [英] wildfly registering mysql as a datasource
问题描述
我一直在尝试将mysql配置为wildfly中的数据源.我不确定我错过了什么,我在启动时遇到错误.
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-connector-java-5.0.8-bin.jar和module.xml: "/wildfly-8.1.0.Final/modules/system/layers/base/com/mysql/main"
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"
下面是文件
module.xml
module.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
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>
我已经使用eclipse中的独立程序测试了jdbc的连通性,并且可以正常工作
I have tested the jdbc connectivity with a standalone program in eclipse and it worked
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();
}
}
}
参考下面的链接并修改了module.xml
Referred to the below link and modified the module.xml
这是我启动时遇到的错误日志
Here is a the error log i get on start up
02:45:17,169错误[org.jboss.as.controller.management-operation] (控制器引导线程)JBAS014613:操作(添加")失败- 地址: ([ (子系统" =>数据源"), (数据源" =>"proj")])-故障描述:{"JBAS014771:缺少/不可用的依赖项的服务" => [ 缺少jboss.data-source.java:jboss/datasources/proj [jboss.jdbc-driver.mysql]", "jboss.driver-demander.java:jboss/datasources/proj丢失[jboss.jdbc-driver.mysql]"]} 02:45:17,175错误 [org.jboss.as.controller.management-operation](控制器启动 线程)JBAS014613:操作(添加")失败-地址:([ (子系统" =>数据源"), ("data-source" =>"proj")])-失败描述:{ "JBAS014771:服务缺少/不可用的依赖项" => [ 缺少jboss.data-source.java:jboss/datasources/proj [jboss.jdbc-driver.mysql]", 缺少jboss.driver-demander.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"] }
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"] } }
推荐答案
为解决Wildfly上的mysql数据源配置问题,我使用了管理控制台来添加数据源并对其进行测试.
To resolve the mysql datasource configuration issue on Wildfly i used the admin console to add the datasource and test it.
-
如果登录Web控制台并找到您尝试配置为禁用的数据源 那个删除它. standalone.xml和module.xml重置为原始.
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.
配置新的数据源:
名称:mysql
JNDI:java:jboss/datasources/proj
name :mysql
JNDI :java:jboss/datasources/proj
单击下一步,然后输入URL(我在下面使用了),然后单击启用",然后单击测试".
网址:jdbc:mysql://localhost/proj
Click next and enter the url (i used the below) and click enable and then click test
url : jdbc:mysql://localhost/proj
测试应该显示成功
standalone.xml和module.xml的更改是自动进行的.
The changes to the standalone.xml and the module.xml are automatically made .
如果现在重新启动服务器,它应该可以正常启动并且应该可以从Web项目中访问数据库
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
这篇关于Wildfly将mysql注册为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!