JBoss数据源的定义和使用
我使用Jboss作为5.1.x服务器。当我在java程序中使用带有jndi名称的数据源对象时,我在连接到服务器管理器连接池时出错。 请验证下面提到的细节,并给我一些建议,说明为什么会出现错误 为了将数据源放在Jndi中,我们必须将*-ds.xml文件放在default/deploy文件夹中,并使用以下标记JBoss数据源的定义和使用,jboss,Jboss,我使用Jboss作为5.1.x服务器。当我在java程序中使用带有jndi名称的数据源对象时,我在连接到服务器管理器连接池时出错。 请验证下面提到的细节,并给我一些建议,说明为什么会出现错误 为了将数据源放在Jndi中,我们必须将*-ds.xml文件放在default/deploy文件夹中,并使用以下标记 <?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource>
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/mysql</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.
jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class- name>
<new-connection-sql>SELECT 1</new-connection-sql>
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
当我在类文件上面运行时,我得到了下面的错误堆栈跟踪
D:\JAVA DRIVER\JAVA\JNDI>java JndiDsConTest
Exception in thread "main" javax.naming.CommunicationException: Could not obtain
connection to any of these urls: MySqlDS and discovery failed with error: javax
.naming.CommunicationException: Receive timed out [Root exception is java.net.So
cketTimeoutException: Receive timed out] [Root exception is javax.naming.Communi
cationException: Failed to connect to server MySqlDS:1099 [Root exception is jav
ax.naming.ServiceUnavailableException: Failed to connect to server MySqlDS:1099
[Root exception is java.net.UnknownHostException: MySqlDS]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1763)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
at javax.naming.InitialContext.lookup(Unknown Source)
at JndiDsConTest.main(JndiDsConTest.java:25)
Caused by: javax.naming.CommunicationException: Failed to connect to server MySq
lDS:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to
connect to server MySqlDS:1099 [Root exception is java.net.UnknownHostException:
MySqlDS]]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:335)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1734)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server
MySqlDS:1099 [Root exception is java.net.UnknownHostException: MySqlDS]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:305)
... 5 more
Caused by: java.net.UnknownHostException: MySqlDS
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory
.java:81)
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:301)
... 5 more
请给我找出错误的原因并解决
非常感谢。查看stacktrace消息
java.net.UnknownHostException:MySqlDS
,我想JDBC驱动程序正在寻找一个名为“MySqlDS”的主机,所以我认为它不会从mysql-ds.xml文件中获取信息,至少不会像您在这里描述的那样
下面是我在JBoss5.1.0中使用的一些代码。(我显示的是您的数据源名称,而不是我的原始名称。)
我不知道JNDI查找是否区分大小写。我自己也一直用小写
我希望这有帮助 @覆盖
受保护的void doGet(HttpServletRequest-req,HttpServletResponse-resp)抛出ServletException,IOException{
PrintWriter out = resp.getWriter();
resp.setContentType("application/json");
try {
DataSourceFactory dataSourceFactory = new DataSourceFactory();
IDAOFactory dAOFactory = new DAOFactoryImpl(dataSourceFactory);
IServiceFactory serviceFactory = new ServiceFactoryImpl(dAOFactory);
IItemService itemService = serviceFactory.getItemService();
ArrayList<Item> allItems = itemService.getAll();
Gson gson = new GsonBuilder().create();
String list = gson.toJson(allItems);
out.write(list);
} catch (Throwable ex) {
ServletException servletException = new ServletException(ex);
throw servletException;
} finally {
if (out != null) {
out.close();
}
}
}
PrintWriter out=resp.getWriter();
分别为setContentType(“应用程序/json”);
试一试{
DataSourceFactory DataSourceFactory=新的DataSourceFactory();
IDAOFactory dAOFactory=新DAOFactoryImpl(数据源工厂);
IServiceFactory=newServiceFactoryImpl(dAOFactory);
IItemService itemService=serviceFactory.getItemService();
ArrayList allItems=itemService.getAll();
Gson Gson=new GsonBuilder().create();
String list=gson.toJson(allItems);
写出(清单);
}捕获(可丢弃的ex){
ServletException ServletException=新ServletException(ex);
抛出servletException;
}最后{
if(out!=null){
out.close();
}
}
}
函数loadOrders(){
var ajaxConfig={
键入:“获取”,
url:“getAllOrders”,
async:true,
contentType:“”,
数据:“,
数据类型:“json”
};
$.ajax(ajaxConfig)
.完成(功能(响应){
对于(变量i=0;i
}需要注意的几件事-1)确保连接字符串正确,2)确保数据库正常,3)确保使用正确的JDBC驱动程序。是的,所有这些设置都可以,但连接将被拒绝。。
javax.naming.Context ic = new javax.naming.InitialContext();
javax.naming.Context ctx = (javax.naming.Context) ic.lookup("java:");
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/mysqlds");
conn = ds.getConnection();
PrintWriter out = resp.getWriter();
resp.setContentType("application/json");
try {
DataSourceFactory dataSourceFactory = new DataSourceFactory();
IDAOFactory dAOFactory = new DAOFactoryImpl(dataSourceFactory);
IServiceFactory serviceFactory = new ServiceFactoryImpl(dAOFactory);
IItemService itemService = serviceFactory.getItemService();
ArrayList<Item> allItems = itemService.getAll();
Gson gson = new GsonBuilder().create();
String list = gson.toJson(allItems);
out.write(list);
} catch (Throwable ex) {
ServletException servletException = new ServletException(ex);
throw servletException;
} finally {
if (out != null) {
out.close();
}
}
}
var ajaxConfig = {
type: "GET",
url: "getAllOrders",
async: true,
contentType: "",
data: "",
dataType: "json"
};
$.ajax(ajaxConfig)
.done(function (response) {
for (var i = 0; i < response.length; i++) {
var html = "<tr> \
<td>" + response[i].orderId + "</td> \
<td>" + response[i].date + "</td>\
<td>" + response[i].customerId + "</td> \
</tr>";
$("#tbleOrders tbody").append(html);
}
})
.fail(function (xhr, status, errorMessage) {
alert("Faild to load data !!");
});