Eclipse for MS Sql Server 中 Tomcat 的连接池

Connection Pool for Tomcat in Eclipse for MS Sql Server

好的,我正在尝试在 Vaadin 网络应用程序中设置连接池,我听说最好让 Tomcat 执行连接池。所以我有 一直在浏览一些 Youtube 视频和很多教程,但我在这方面太糟糕了,无法让它发挥作用。我真的需要有人帮忙。

SQL 服务器是否必须在我的计算机上?它可以在不同的服务器上吗?

假设我有一个数据库:

我想访问 DB2 中名为 Table1 的 table。所以根据 this 我必须在 ECLIPSE 中进入 server.xml

并添加

    <Resource name="jdbc/ServerName"
        auth="Container"
        factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        type="javax.sql.DataSource"
        maxActive="50"
        maxIdle="10"
        maxWait="15000"
        username="username"
        password="password"
        url="jdbc:sqlserver://ServerName;instance=SQLEXPRESS;databaseName=DB2;"
        removeAbandoned="true"
        removeAbandonedTimeout="30"
        logAbandoned="true" />     

但是根据 this guy, 它应该在 Context.xml 中(我已经试过了 context.xml,我只试过 Server.xml,我也试过使用这个资源代码。)我认为它要求 META-INF > Context.xml 在我的 Vaadin 网络应用程序中,我找不到。这是我唯一的 Meta-Inf 文件夹,当我在这里创建一个 context.xml 文件时,当我尝试编译它时它就消失了。

无论如何,为了继续本教程,我需要将驱动程序添加到我的 Tomcat>Lib 文件夹中,所以我下载了所有 these drivers 并添加它们。

只是为了更好的衡量,因为其他一些人正在使用 JDBC driver 我下载它并将其添加到我的 Tomcat > Lib 中。

跟随 these notes 一个在他的 Vaadin 程序中使用连接池的人,我将其添加到我的 java 代码中:

Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Context ctx = (Context) new InitialContext().lookup("java:comp/env");
            BasicDataSource ds = (BasicDataSource)ctx.lookup("jdbc/ServerName");
            con = ds.getConnection();

            String sql = "select * from DB2.dbo.Table1 where [Field1] IS NULL";     
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            //Add in all info
            while (rs.next()){
                beanResultsList.addBean(new Bean(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getDate(4),
                        rs.getDate(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getInt(8),
                        rs.getInt(9),
                        rs.getString(10),
                        rs.getString(11),
                        rs.getString(12),
                        rs.getString(13)
                        )); 
            }
            } catch (SQLException | NamingException e) {
                e.printStackTrace();
            }finally{
                try { con.close();  } catch (SQLException e) {}
                try { rs.close();   } catch (SQLException | NullPointerException e) {}
                try { stmt.close(); } catch (SQLException e) {}
            } // End finally (try catch)

当我 运行 时,我在 finally 块的 con.close() 处得到 NullPointerException,因为它没有拉 table。或者,我一开始就没有联系。

老实说,我不知道自己在做什么。我不知道如果 运行 没有关闭我的计算机,是否可以连接到 sql 服务器?我看到的每个示例都使用 Localhost。

编辑: 作为参考,以下代码可以 100% 零问题运行,但我无法使上面的连接池运行。

Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://ServerName;instance=SQLEXPRESS","username","password");

            String sql = "select * from DB2.dbo.Table1 where [Field1] IS NULL";     
                stmt = con.createStatement();
                rs = stmt.executeQuery(sql);
                //Add in all info
                while (rs.next()){
                    beanResultsList.addBean(new Bean(
                            rs.getInt(1),
                            rs.getString(2),
                            rs.getString(3),
                            rs.getDate(4),
                            rs.getDate(5),
                            rs.getInt(6),
                            rs.getInt(7),
                            rs.getInt(8),
                            rs.getInt(9),
                            rs.getString(10),
                            rs.getString(11),
                            rs.getString(12),
                            rs.getString(13)
                            )); 
                }
                } catch (SQLException | NamingException e) {
                    e.printStackTrace();
                }finally{
                    try { con.close();  } catch (SQLException e) {}
                    try { rs.close();   } catch (SQLException | NullPointerException e) {}
                    try { stmt.close(); } catch (SQLException e) {}
                } // End finally (try catch)

我建议你放慢速度。这个问题是几个问题的组合。一次只关注一个问题,找到你的答案,当你看到它们如何适合时,把这些部分放在一起。

首先,"Does the SQL server have to be on my computer? Can it be on a different server?"。它可以位于不同的服务器上,只要您可以通过网络访问它。

其次,您所指的"Anyway, trying to continue with the tutorial, I need to add the drivers to my Tomcat>Lib folder so I download all of these drivers and add them.""These drivers"是JDBC司机。如果您连接到 Microsoft SQL 服务器,那么您所指的额外 'JDBC' 驱动程序对您没有用,因为它们是 MySQL JDBC 驱动程序(这完全是不同于 SQL 服务器的数据库)。

第三,你显然在使用 Maven,当你添加一个 META-INF 文件夹和一个 context.xml 时,它会被删除,因为 Maven 项目中 'target' 文件夹中的所有内容都会被删除在 运行 Maven 构建时重建。您需要将 META-INF 文件夹添加到 /src/main/webapp/ 文件夹。然后 context.xml 文件将在构建时复制到您最初尝试放置它并希望它保留的位置。

我认为定义 JNDI 数据源的正确位置是在 server.xml 中,在 context.xml 中有引用请参阅此答案 Tomcat JNDI resource name aliases