2016-03-27 150 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; 
}