Java 通过web控制台向wildfly添加数据源
我已经找了好几天了,再也找不到了。 我试图通过web控制台将MySQL数据源分配给我的Wildfly 9服务器。 如果我添加资源,我将在测试资源时出错,因为wildfly 9中存在一个bug,该bug创建了最大池大小为0的资源。 所以我自己编辑了它,一切看起来都很好。 现在,当我尝试在我的webapp中查找该资源时,它只是一个生成一些json数据的经典servlet应用程序,我得到了一个错误。 javax.naming.NameNotFoundException。 资源的jndi名称是:java:/MySQLDS 这是通过web控制台生成的XML:Java 通过web控制台向wildfly添加数据源,java,mysql,jndi,wildfly,Java,Mysql,Jndi,Wildfly,我已经找了好几天了,再也找不到了。 我试图通过web控制台将MySQL数据源分配给我的Wildfly 9服务器。 如果我添加资源,我将在测试资源时出错,因为wildfly 9中存在一个bug,该bug创建了最大池大小为0的资源。 所以我自己编辑了它,一切看起来都很好。 现在,当我尝试在我的webapp中查找该资源时,它只是一个生成一些json数据的经典servlet应用程序,我得到了一个错误。 javax.naming.NameNotFoundException。 资源的jndi名称是:java
<datasource jta="true" jndi-name="java:/MySQLDS" pool-name="MySQLDS" enabled="true" use-ccm="true" statistics-enabled="false">
<connection-url>jdbc:mysql://ip:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<pool>
<min-pool-size>0</min-pool-size>
<initial-pool-size>0</initial-pool-size>
<max-pool-size>20</max-pool-size>
<allow-multiple-users>false</allow-multiple-users>
</pool>
<security>
<user-name>user</user-name>
<password>password</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<validate-on-match>false</validate-on-match>
<background-validation>true</background-validation>
<use-fast-fail>false</use-fast-fail>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
jdbc:mysql://ip:3306/test
com.mysql.jdbc.Driver
mysql
0
0
20
假的
用户
密码
假的
真的
假的
假的
0
0
0
0
0
0
假的
在我的应用程序中,我有一个类,出于各种原因在hashmap中管理我的连接
public class DBConnection {
static {
try {
context = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
private static InitialContext context;
/**
* JNDI Lookup cache
*/
private static final HashMap<String, DataSource> connectionCache = new HashMap<String, DataSource>();
private DBConnection() {
}
public static Connection getConnection(String shema) {
DataSource ds = connectionCache.get(shema);
if (ds == null) {
try {
ds = (DataSource) context.lookup("java:/" + shema);
} catch (NamingException e) {
e.printStackTrace();
}
connectionCache.put(shema, ds);
}
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
公共类数据库连接{
静止的{
试一试{
context=新的InitialContext();
}捕获(NamingE例外){
e、 printStackTrace();
}
}
私有静态初始上下文;
/**
*JNDI查找缓存
*/
私有静态最终HashMap connectionCache=新HashMap();
专用数据库连接(){
}
公共静态连接getConnection(字符串shema){
DataSource ds=connectionCache.get(shema);
如果(ds==null){
试一试{
ds=(DataSource)context.lookup(“java:/”+shema);
}捕获(NamingE例外){
e、 printStackTrace();
}
连接缓存。put(shema,ds);
}
试一试{
返回ds.getConnection();
}捕获(SQLE异常){
e、 printStackTrace();
}
返回null;
}
}
这会产生上述错误
奇怪的是:
我从web控制台中删除了该资源,并通过我的web-INF文件夹中的一个*-ds.xml文件添加了完全相同的资源,它工作正常
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
<datasource jndi-name="java:/MySQLDS" pool-name="MySQLPOOL">
<connection-url>jdbc:mysql://ip:3306/test</connection-url>
<driver>mysql</driver>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<security>
<user-name>user</user-name>
<password>password</password>
</security>
</datasource>
</datasources>
jdbc:mysql://ip:3306/test
mysql
com.mysql.jdbc.Driver
用户
密码
问题是我如何通过web控制台添加资源并在我的应用程序中查找?
如果我通过xml而不是通过web控制台添加资源,为什么我能够找到资源?这是我的数据源,它工作正常(通过web控制台创建)
jdbc:mysql://localhost:3306/users?autoReconnect=true&;useUnicode=true&;amp;characterEncoding=utf8&;amp;characterSetResults=utf8&;amp;ConnectionCallation=utf8\U unicode\U ci;useLegacyDatetimeCode=false
com.mysql.jdbc.Driver
mysql-connector-java-5.1.35.jar_com.mysql.jdbc.Driver_5_1
事务\u已读\u未提交
1.
0
256
假的
用户
我的密码
假的
真的
假的
假的
0
0
0
0
0
0
真的
128
真的
好吧,现在我觉得自己有点迟钝
我只需要重新加载服务器配置。
我想只要添加资源就足够了,你就可以开始了
在Wildfly内部的一个页面上,它表示必须重新加载配置。所以我做了,现在一切都正常了
很抱歉,这是一个垃圾堆。这不是哑巴,有点混乱,我还得到了“无法创建jdbc连接-需要重新加载”-但我花了一段时间才找到这个“重新加载”按钮。我刚刚更新到cr2,现在web控制台全乱了:现在它看起来像一个移动视图。而且很多标签是重叠的-而且重新加载按钮都没有了,这并不意味着你必须重新加载服务器。现在,您需要重新启动整个服务。。该死的。有人有同样的问题吗?欢迎来到俱乐部:
<datasource jta="true" jndi-name="java:/jdbc/NotesUsersDS" pool-name="NotesUsersDS" enabled="true" use-ccm="true" statistics-enabled="true">
<connection-url>jdbc:mysql://localhost:3306/users?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8&amp;characterSetResults=utf8&amp;connectionCollation=utf8_unicode_ci;useLegacyDatetimeCode=false</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql-connector-java-5.1.35.jar_com.mysql.jdbc.Driver_5_1</driver>
<transaction-isolation>TRANSACTION_READ_UNCOMMITTED</transaction-isolation>
<pool>
<min-pool-size>1</min-pool-size>
<initial-pool-size>0</initial-pool-size>
<max-pool-size>256</max-pool-size>
<allow-multiple-users>false</allow-multiple-users>
</pool>
<security>
<user-name>users_user</user-name>
<password>my_password</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<validate-on-match>false</validate-on-match>
<background-validation>true</background-validation>
<use-fast-fail>false</use-fast-fail>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<track-statements>true</track-statements>
<prepared-statement-cache-size>128</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>