首页 \ 问答 \ 为什么我们的连接池关闭?(Why is our Connection Pool Closing?)

为什么我们的连接池关闭?(Why is our Connection Pool Closing?)

我们有一台运行在Windows Server 2008 R2上运行的Apache Tomcat 7.0.54上的JSF2.0网络服务器。 我们在运行的计算机上安装了2台SQL服务器,另一台托管了我们的库存软件。 我们网页的一部分是验证PartNumbers被添加。 在阅读了Connection Pools是与SQL服务器交流的最佳实践之后,我们创建了一个并用它来对我们的库存软件进行一些验证。

因为我想要一种方法来检查连接池的健康状况,我使用ViewScoped支持bean创建了一个测试页面,验证2个已知的良好部件号。 今天是本周第二次出现错误消息“连接已关闭”。 由于我是新手,并且似乎无法找到关于此的任何信息,所以我对我们没有设置正确的东西感到困惑。 我刚刚重置了Apache服务器,并且它已备份并正在运行。 所以..要创建我们的连接池,我在应用程序的META-INF / context.xml中添加了一些代码。

    <Resource type="javax.sql.DataSource"
        name="jdbc/FOODB"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://Foobar\Inventory;databaseName=FooInventory;user=johnDoe;password=astrongpassword;"
    />

我必须使用ConnectionPool的基本概念是我有一个名为SqlAccessCommand的接口。 它或多或少是一种适配器模式。 在我的测试页面中,我使用RunUnsafe方法,以便可以显示错误消息。 所以这里是RunUnsafe方法。

protected static DataSource getDataSource() throws NamingException {
    Context context = (Context) new InitialContext().lookup("java:/comp/env");
    DataSource ds = (DataSource) context.lookup("jdbc/FOODB");
    return ds;
}
public static <T> T RunUnsafe(SqlAccessCommand<T> command) throws NamingException, SQLException {
    try {
        DataSource ds = getDataSource();
        try (Connection connection = ds.getConnection();
                PreparedStatement statement = connection.prepareStatement(command.getSqlStatement())) {
            command.prepareStatment(statement);
            try (ResultSet rs = statement.executeQuery()) {
                return command.getResults(rs);
            }
        }
    } catch (NamingException | SQLException e) {
        Logger.getLogger(AOSqlInformationHolder.class.getName()).log(Level.SEVERE, null, e);
        throw e;
    }
}

正如你所看到的,我使用了试用资源,它应该在使用后关闭我的连接,无论如何。 就像try / catch /最后一样,只是更清洁(IMO)。 所以当我的连接打开时,这工作得很好。 到目前为止2x现在我不得不重新启动服务器(因为我不知道如何以其他方式重新打开连接)我错过了什么? 如果需要拼图的更多部分给我留言,我会发布我能做的。 谢谢。

编辑

只是看了看日志文件,今天早上抛出了这个异常

com.microsoft.sqlserver.jdbc.SQLServerException:由peer重置的连接:套接字写入错误

根据迄今为止的评论,它被假定为暂停。 这个错误信息仍然指向那个方向吗?


We have a JSF2.0 webserver running on Apache Tomcat 7.0.54 running on Windows Server 2008 R2. We have 2 SQL servers on on the machine this is running on, and another which hosts our inventory software. One part of our webpage is validation of PartNumbers being added. After reading that Connection Pools are best practice for talking with SQL servers we created one and used it for some of the validation against our inventory software.

Because I wanted a way to be able to check the health of the connection pool I created a test page with a ViewScoped backing bean that validates 2 known good part numbers. Today is the second time this week that the error message "Connection is closed" appeared. Since i'm new to conenction pools and can't seem to find any information regarding this I'm baffled as to what we didn't setup right. I just now reset the Apache server and it is back up and running. So.. to create our connection pool I added some code to the app's META-INF/context.xml.

    <Resource type="javax.sql.DataSource"
        name="jdbc/FOODB"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://Foobar\Inventory;databaseName=FooInventory;user=johnDoe;password=astrongpassword;"
    />

The basic concept that I have to use the ConnectionPool is that I have an interface called SqlAccessCommand. It is more or less an adapter pattern. In my test page i use the RunUnsafe method so that I can display the error message. So here is the RunUnsafe method.

protected static DataSource getDataSource() throws NamingException {
    Context context = (Context) new InitialContext().lookup("java:/comp/env");
    DataSource ds = (DataSource) context.lookup("jdbc/FOODB");
    return ds;
}
public static <T> T RunUnsafe(SqlAccessCommand<T> command) throws NamingException, SQLException {
    try {
        DataSource ds = getDataSource();
        try (Connection connection = ds.getConnection();
                PreparedStatement statement = connection.prepareStatement(command.getSqlStatement())) {
            command.prepareStatment(statement);
            try (ResultSet rs = statement.executeQuery()) {
                return command.getResults(rs);
            }
        }
    } catch (NamingException | SQLException e) {
        Logger.getLogger(AOSqlInformationHolder.class.getName()).log(Level.SEVERE, null, e);
        throw e;
    }
}

As you can see i use the try with resources which is supposed to close my connections after use no matter what. Much like the try/catch/finally does, just cleaner (IMO). so when my connection is open this works just fine. so far 2x now I have had to restart the server (since I don't know how to re-open said connection any other way) what am I missing? If more parts of the puzzle are needed leave me a comment and I will post what I can. Thank you.

EDIT

just looked into the log files and this morning there was this exception thrown

com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error

According to the comments thus far it was assumed a timeout. Does this error message still point in that direction?


原文:https://stackoverflow.com/questions/27024861
更新时间:2022-11-06 21:11

最满意答案

在您的MYSQL查询中:

SELECT * FROM `yourTableName` WHERE `eventDate` >= $startDate AND `eventDate` <= $endDate

PS:我不确定引用查询中的变量的引号。

PPS:从不使用*来选择列,始终只选择您需要的列。 我在这里使用它是因为我不知道列的名称


I ended up doing my checking in PHP and print a new row only when a different date is detected.

Codes below in case it serves someone's needs in future.

<?php
        $currentPrintDay = 0;
        $currentPrintMonth = 0;
        $currentPrintYear = 0;

        echo "<table>"
        foreach($reservationsToShow as $row):
        // get day, month, year of this entry
        $timestamp = strtotime($row['timestamp']);
        $day = date('d', $timestamp);
        $month = date('m', $timestamp);
        $year = date('Y', $timestamp);

        // if it does not match the current printing date, assign it to the current printing date,
        // assign it, print a new row as the header before continuing
        if($day != $currentPrintDay || $month != $currentPrintMonth || $year != $currentPrintYear) {
            $currentPrintDay = $day;
            $currentPrintMonth = $month;
            $currentPrintYear = $year;

            echo 
            "<tr>" .
            "<td colspan='100%'>". date('d-m-Y', $timestamp) . "</td>" .
            "</tr>";
        }
        // continue to print event details from here on...
?>

相关问答

更多
  • 我能够让这个工作,我认为这与日期/时间格式有关,而不是任何事情。 $event_day = date('Y-m-d',strtotime($year.'-'.$month.'-'.$list_day)); foreach($events as $event) { if ($event[ArrDate] == $event_day) { $cntry = $event[CountryID]; ...
  • 您需要将$calendarList作为Object获取,而不是像当前代码那样获取Array。 使用 $client->setUseObjects(true); 就在此之前 $service = new apiCalendarService($client); 说明: $events = $cal->events->listEvents('rtparies@gmail.com'); 返回一个数组。 如果您不需要对象进行进一步处理,那就没问题。 由于您需要一个对象,因此需要将对象的使用设置为true: ...
  • 相关文章

    更多

    最新问答

    更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)