2016-03-27 73 views
0

我想配置Tomcat 8 PostgreSQL的配置Tomcat 8 Postgres的

我加入這個在context.xml中

<Resource name="jdbc/DefaultDB" auth="Container" type="javax.sql.DataSource" 
      username="postgres" password="qwerty" 
      url="jdbc:postgresql://localhost:5432/crm" 
      driverClassName="org.postgresql.Driver" 
      initialSize="5" maxWait="5000" 
      maxActive="120" maxIdle="5" 
      validationQuery="select 1" 
      poolPreparedStatements="true"/> 

我試着運行這個Java代碼:

public String init() 
    { 
     String user_name = null; 
     try 
     { 
      Context ctx = new InitialContext(); 
      if (ctx == null) 
       throw new Exception("Boom - No Context"); 

      DataSource ds = (DataSource) ctx.lookup("jdbc/DefaultDB"); 

      if (ds != null) 
      { 
       Connection conn = ds.getConnection(); 

       if (conn != null) 
       { 
        Statement stmt = conn.createStatement(); 
        ResultSet rst = stmt.executeQuery("select id, user_name from user where username = " + user); 
        if (rst.next()) 
        { 
         user_name = rst.getString("user_name"); 
        } 
        conn.close(); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return user_name; 
    } 

但由於某些原因,我添加此代碼後,Tomcat並未啓動。你有什麼想法我錯了嗎?

我得到這個在Tomcat的日誌文件:

28-Mar-2016 10:37:07.955 WARNING [localhost-startStop-1] org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory.getObjectInstance Name = DefaultDB Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "120" for "maxActive" property, which is being ignored. 
    28-Mar-2016 10:37:07.956 WARNING [localhost-startStop-1] org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory.getObjectInstance Name = DefaultDB Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "5000" for "maxWait" property, which is being ignored. 

javax.naming.NameNotFoundException: Name [jdbc/DefaultDB] is not bound in this Context. Unable to find [jdbc]. 
+0

你得到的錯誤是什麼? Tomcat日誌文件中必須記錄一些內容。 –

+0

我添加了一些日誌文件輸出 –

+0

你的查找是錯誤的:https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#Using_resources –

回答

1

爲@a_horse_with_no_name說,你的查詢是錯誤的。你的代碼必須是這樣的:

public String init() 
{ 
    String user_name = null; 
    try 
    { 
     Context ctx = new InitialContext(); 
     if (ctx == null) 
      throw new Exception("Boom - No Context"); 
     Context envCtx = (Context) ctx.lookup("java:comp/env"); 
     DataSource ds = (DataSource) envCtx.lookup("jdbc/DefaultDB"); 

     if (ds != null) 
     { 
      Connection conn = ds.getConnection(); 

      if (conn != null) 
      { 
       Statement stmt = conn.createStatement(); 
       ResultSet rst = stmt.executeQuery("select id, user_name from user where username = " + user); 
       if (rst.next()) 
       { 
        user_name = rst.getString("user_name"); 
       } 
       conn.close(); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return user_name; 
}