我使用的是Jboss AS 5.1.x服务器。当我在我的java程序中使用带有jndi名称的DataSource对象时,我在连接到服务器管理器连接池时遇到错误。 请验证下面提到的详细信息,并告诉我为什么会出错。
要将DataSource放置到Jndi,我们必须放置* -ds.xml文件,这样我就将mysql-ds.xml文件放在带有follwed标签的default / deploy文件夹中。
<?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>
将此xml文件放在deploy文件夹中后,当我浏览Jndiview时,我在jnditree中找到了一个带有MySqlDs的标签: | MySqlDs
对于使用上面绑定的jndi名称,我使用了以下的java程序(独立)。
// JndiDsConTest.java
import javax.naming.*; //(jndi api)
import javax.sql.*;
import java.sql.*;
import java.util.*;
public class JndiDsConTest
{
public static void main(String args[])throws Exception
{
//Jbosss
Hashtable ht=new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
ht.put(Context.PROVIDER_URL,"jnp://localhost:1099");
// create InitialContext obj
InitialContext ic=new InitialContext(ht);
// represents conectivity with registry s/w
// get DataSoruce obj ref from registry
DataSource ds=(DataSource)ic.lookup("java:/MySqlDS");
//DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/MySqlDS");
// get con obj from Jdbc Con pool
Connection con=ds.getConnection();
// write jdbc persistance logic
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select count(*) from employee");
if(rs.next())
{
System.out.println("count of records :"+rs.getInt(1));
}
// close jdbc objs
rs.close();
st.close();
// release con obj back to con pool
con.close();
}//main
}//class
当我在类文件上运行时,我得到了错误的堆栈跟踪。
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
请找出错误堆栈跟踪的原因并解决它。
谢谢你。
答案 0 :(得分:0)
查看stacktrace消息java.net.UnknownHostException: MySqlDS
,我会说JDBC驱动程序正在寻找名为“MySqlDS”的主机,所以我认为它不会从你的mysql-ds.xml文件中获取信息,至少不是你在这里描述它的方式。
这是我在JBoss 5.1.0中运行的一些代码。 (我显示的是您的数据源名称而不是原始名称。)
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();
在我的脑海中,我不知道JNDI查找是否区分大小写。我自己总是使用小写字母。
我希望有所帮助。
答案 1 :(得分:0)
@覆盖 protected 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();
}
}
}
function loadOrders(){
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 !!");
});
}