【问题标题】:Setting up connection pool with Tomcat 7 (MySQL)使用 Tomcat 7 (MySQL) 设置连接池
【发布时间】:2015-10-31 17:01:42
【问题描述】:

我正在使用 Tomcat 7.0.52 和 mysql-connector-java-5.1.36.jar。 我将 mysql jar 复制到 tomcat/lib 文件夹中。

DriverManager 工作,我可以获取连接并执行查询:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, usr, pw);
Statement stmnt = con.createStatement();
ResultSet rs = stmnt.executeQuery("SELECT * FROM county");

但我无法让它与连接池一起使用。

MyWebApp.war/WEB-INF/classes/Servu.class(此应用中唯一的类,一个 servlet):

public class Servu extends HttpServlet {

    private static DataSource ds;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Context ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("jdbc/maakler_new");
        } catch (ClassNotFoundException | NamingException ex) {
            Logger.getLogger(Servu.class.getName()).log(Level.SEVERE, "thrown from init()", ex);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Table:\n");
        try (PrintWriter out = resp.getWriter();
             Connection c = ds.getConnection();
             Statement stmnt = c.createStatement();
             ResultSet rs = stmnt.executeQuery("SELECT * FROM county");
        ) {
            while (rs.next()) {
                out.print(rs.getString(2) + "\n");
            }
            out.flush();
        } catch (SQLException ex) {
            Logger.getLogger(Servu.class.getName()).log(Level.SEVERE, "thrown from doGet()", ex);
        }
    }
}

MyWebApp.war\META-INF\context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>

      <Resource name="jdbc/maakler_new" auth="Container" type="javax.sql.DataSource"
               maxActive="40" maxIdle="30" maxWait="15000"
               username="user" password="pw" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://mydblocation.ee:3306/maakler_new"/>

</Context>

MyWebApp.war\WEB-INF\web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
            <servlet-name>Servu</servlet-name>
            <servlet-class>Servu</servlet-class>
    </servlet>
    <servlet-mapping>
            <servlet-name>Servu</servlet-name>
            <url-pattern>/Servu</url-pattern>
    </servlet-mapping> 

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/maakler_new</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

</web-app>

如果我部署应用程序,我会收到此错误(来自 tomcat/logs/tomcat7-stderr.2015-10-31.log):

SEVERE: thrown from init()
javax.naming.NameNotFoundException: Name [jdbc/maakler_new] is not bound in this Context. Unable to find [jdbc].

为什么会出现此异常以及如何解决?

编辑1: 将 ResourceLink 添加到上下文中,但仍然没有:

<Context>
            ...

       <ResourceLink global="jdbc/maakler_new" name="jdbc/maakler_new" type="javax.sql.DataSource" />

</Context>

编辑2: 这是我现在的 tomcat/conf/server.xml 文件:

<?xml version='1.0' encoding='utf-8'?>

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/maakler_new" auth="Container" type="javax.sql.DataSource"
               maxActive="40" maxIdle="30" maxWait="15000"
               username="usr" password="pw" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://mydblocation.ee:3306/maakler_new"/>

  </GlobalNamingResources>

...

【问题讨论】:

    标签: java mysql tomcat connection-pooling


    【解决方案1】:

    问题出在 Java 代码中。我正在从错误的上下文(根上下文)中寻找数据源。这是错误的:

    Context ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("jdbc/maakler_new");
    

    正确的方法是:

    Context ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("java:comp/env/jdbc/maakler_new");
    

    【讨论】:

      【解决方案2】:

      您在 server.xml 中缺少一个条目。这应该会有所帮助

      http://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-connection-pool-configuration-example/

      【讨论】:

      • 我正在阅读指南:“对于每个 Web 应用程序资源配置,需要在 TOMCAT_ROOT_DIR\webapps\PROJECT_DIR\META-INF 中创建文件 context.xml 以添加“资源”条目。”我不是已经有了这个吗?我也会尝试 server.xml 条目,尽管我不希望所有 webapps 都可以访问连接池。
      • 我在 tomcat/conf/server.xml 中添加了完全相同的 元素,但仍然得到相同的 NameNotFoundException。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2015-02-20
      • 2018-03-23
      • 2012-10-14
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多