独立的java类无法从tomcat7

时间:2016-02-12 16:55:59

标签: postgresql jsp ubuntu tomcat7 remote-access

我有一个程序,我正在移植到Web界面。当我独立运行时它连接到我的postgres数据库,但是当我在Tomcat中安装jar然后将类导入我的JSP时,连接尝试只是旋转。

我正在运行Tomat7。有什么东西我必须添加到tomcat以便允许连接或我是否必须修改我的数据库管理器以通过tomcat连接?

我找到了这个页面,但它是针对从JSP连接的。我的所有连接都在我的jarfile中的类中完成。我没有必要从JSP查询我已经编写了DAO和DTO。

https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

我修改了连接器类以从连接池获取连接。这是我的连接管理器(是的,我知道我的错误处理需要改进):

public DatabaseManager() {
    p.setUrl(url);java.sql.SQLException: Driver:org.postgresql.Driver@789caeb2 returned null for URL:postgres
    p.setDriverClassName("org.postgresql.Driver");
    p.setUsername(user);
    p.setPassword(password);
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors(
            "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
            "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
          datasource.setPoolProperties(p);
}

public Connection getConnection() throws SQLException {

    con = datasource.getConnection();
    return con;
}

public void close(Connection c) {
    con = c;
    try {
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是我的WEB-INF / web.xml(名称已更改以保护有罪):

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
     <description>My Cool App</description>
    <resource-ref>
       <description>Cool Data Source</description>
       <res-ref-name>jdbc/postgres</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
 </web-app>

这是我在tomcat7 / conf:

中存储的context.xml
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <Manager pathname="" />


    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

<Resource name="jdbc/postgres" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://127.0.0.1:5432/cooldb"
          username="cooluser" password="coolerpass" maxActive="20" maxIdle="10"
maxWait="-1"/>
<ResourceLink global="jdbc/postgres" name="jdbc/postgres" type="javax.sql.DataSource"/>


</Context>

最后但并非最不重要的是,这是一个来自JSP的片段,它调用由于各种原因连接到数据库的类并返回数据的ArrayList:

GenerateByFrequencyAndUnique gen = new GenerateByFrequencyAndUnique();
ArrayList<String> coolList = gen.generateMyListOfCoolStuff();

代码在Eclipse中执行得很好..当它部署它只是旋转时,在catalina.out文件中,在jsp最终超时前10分钟内抛出以下异常约10次。

WARNING: Connection has been abandoned PooledConnection[org.postgresql.jdbc.PgConnection@15f9c366]:java.lang.Exception
at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1072)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:789)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:628)
at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:187)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
at org.apache.jsp.generateSets_jsp._jspService(generateSets_jsp.java:139)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

另一个有趣的事实是,我在带有Ubuntu的联想ThinkServer上运行它。我有verizon互联网,以便让应用程序可以查看公众我为服务器创建了一个静态IP地址,然后我进入verizon路由器管理器并打开一个端口转发,因此进入verizon池连接的端口80上的请求将被转发到服务器..但是浏览器中的url显示了verizon url。

我已将静态IP和verizon IPV6地址添加到postgres配置文件并重新启动数据库,但它没有帮助。

2 个答案:

答案 0 :(得分:0)

我无法发布此评论作为评论,所以我只是写下我认为你应该做的事情。

虽然您可以在JSP文件中编写所有内容,但它很麻烦,您可以通过创建servlet来帮助自己。

public class Your_servlet_class extends HttpServlet {


 public Connection getConnection() throws SQLException {

con = datasource.getConnection();
return con;
}

 //overriden 
 public void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws IOException, ServletException  {   
 //logic for http get requests

  Connection conn = getConnection();

  //now get some objects from the database (e.g in a List) and set a request   attribute
 request.setAttribute("someAttrName", list);//e.g. list of strings
  //someAttrName can now be used in your jsp page
  getServletContext().getRequestDispatcher("/SomePage.jsp").forward(request, response); 
}
 //overriden 
 public void doPost(HttpServletRequest request, HttpServletResponse 
        response) throws IOException, ServletException  {   
 //logic for http post requests

  Connection conn = getConnection();
  //now change some objects in the database and return SomePage.jsp to the user

 getServletContext().getRequestDispatcher("/SomePage.jsp").forward(request, response);  
}
}

答案 1 :(得分:0)

因此,问题是由我正在研究的Verizon路由器引起的。我使用Verizon配置工具为路由器设置端口转发..问题是我没有通过该路由器发送我的数据库请求..我试图去127.0.0.1,一旦我改变它通过Verizon URL并转发到数据库..它的工作原理。

我必须在路由器中添加端口转发规则,以允许对数据库端口的请求转到数据库。