2013-03-28 20 views
0

我正在使用Jboss AS 5.1.x服務器。當我在我的java程序中使用帶有jndi名稱的DataSource對象時,我在連接到服務器管理器連接池時出錯。 請驗證下面提到的細節,並給我建議爲什麼它給錯誤。JBoss-DataSource Cration和用法

要將DataSource放置到Jndi,我們必須放置* -ds.xml文件,這樣我就可以將mysql-ds.xml文件放在默認/ deploy文件夾中,並帶有follwed標籤。

<?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文件在部署文件夾後,當我瀏覽關於JNDIView我找到了在jnditree一個標籤在Java MySqlDs: | MySqlDs

對於使用上述綁定JNDI名稱我用了follwed的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 

當我運行在類文件上面時,我得到以下錯誤stacktrace。

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錯誤的原因並解決它。

ThankYou in adavance。

+0

很少有東西看出來for - 1)確保連接字符串是正確的,2)使數據庫已啓動,3)確保使用正確的JDBC驅動程序。 – CoolBeans

+0

是的,所有這些設置都很好,但連接將被拒絕.. – user2214448

回答

0

看着堆棧跟蹤信息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查找是區分大小寫的。我一直使用小寫字母。

我希望有幫助。

0

@覆蓋 保護無效的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(); 
     } 
    } 

} 

功能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 !!"); 
     }); 

}