简体   繁体   English

Tomcat 5.5和6.0中的数据损坏问题运行功能

[英]Data Corruption issue running function in Tomcat 5.5 & 6.0

I've been trying to figure out this bug for few days. 我一直在努力找出这个错误几天。 I have narrowed the issue down to a test case as follows. 我将问题缩小到一个测试案例,如下所示。 Again, this is a test case that causes the bug to happen. 同样,这是导致该错误发生的测试用例。 The function (at this point) is not a practical one, but just trying to figure out the bug. 该功能(此时)不是实用功能,而只是试图找出错误。

Server is running: - Tomcat 6 - OpenJDK 1.6.0_17 - CentOS 5.5 服务器正在运行:-Tomcat 6-OpenJDK 1.6.0_17-CentOS 5.5

I have a simple Class file with the following method Static Method and Static variable declaration: 我有一个简单的Class文件,其中包含以下方法“静态方法”和“静态变量声明”:

public static java.text.SimpleDateFormat displayDateSDF1 = new java.text.SimpleDateFormat("MM/dd/yyyy");

public static java.util.Date getSubDateMini(String inputDate)
{
    java.util.Date testObj = null;
    try
    {
        testObj = displayDateSDF1.parse("01/01/2000") ;
    }
    catch (Exception e)
    {
    }
    return testObj;
}

Testing in Tomcat: 在Tomcat中进行测试:

When I run this method, I'd expect to get the same result every time, right? 当我运行此方法时,我希望每次都能获得相同的结果,对吗? However, if I call this method from a JSP, I get the expected result of a Date object with value 1/1/2000 about 99.9% of the time. 但是,如果我从JSP调用此方法,则大约有99.9%的时间会得到Date对象的预期结果,该对象的值为1/1/2000。 However, sometimes I get an unexpected data object passed back with a seemingly random date value. 但是,有时我会收到一个带有看似随机的日期值的意外数据对象。

To test this, I created a JSP with the following code segment in it: 为了对此进行测试,我创建了一个JSP,其中包含以下代码段:

for (int i=0; i<200000;i++)
{
    java.util.Date testObjLib = TestDate.getSubDateMini("") ;
    if (testObjLib!=null&&!testObjLib.toString().equalsIgnoreCase("Sat Jan 01 00:00:00 PST 2000"))
    {
            out.print("<br>"+testObjLib+"");
    }
}

Some dates that appear are as follows: 出现的一些日期如下:

Wed Jan 01 00:00:00 PST 1 1月1日星期三00:00:00 PST 1

Fri Aug 01 00:00:00 PDT 2166 星期五8月1日00:00:00 PDT 2166

In 200,000 runs, I get approximately 50 bad dates which gives an error rate of ~0.025% but again it's random. 在200,000次运行中,我得到大约50个错误的日期,这会导致〜0.025%的错误率,但还是随机的。 I've run this loop with 10 iterations and received an error. 我已经用10次迭代运行此循环,并收到一个错误。 Sometimes it runs the loop with 200,000 and all dates look good. 有时,它以20万个循环运行,所有日期看起来都不错。

Testing in Java: 用Java测试:

Running this loop via a console/terminal app in CentOS with the same loop, I have not seen this error happen yet. 通过CentOS中的控制台/终端应用程序以相同的循环运行此循环,我还没有看到此错误的发生。 I increased the loop to 10,000,000 and had no false results as of yet. 我将循环数增加到10,000,000,到目前为止还没有错误结果。


I can understand out of memory or some thrown error (which would result in a null value) but not corrupt/inconsistent data. 我可以理解内存不足或某些抛出的错误(这将导致空值),但不能理解损坏/不一致的数据。 I built up a new server from scratch with Tomcat 6 and also tried Tomcat 5.5 and both have the same results. 我使用Tomcat 6从零开始构建了一个新服务器,还尝试了Tomcat 5.5,两者的结果相同。 I have not tried Tomcat 7. 我还没有尝试过Tomcat 7。

Any suggestions? 有什么建议么?

SimpleDateFormat is not thread-safe. SimpleDateFormat不是线程安全的。

This means that when it is accessed from multiple threads, unexpected results can be observed. 这意味着当从多个线程访问它时,会观察到意外的结果。 And tomcat is serving each request from a separate thread, so whenever two requests are made at the same time, the problem would arise. 而且tomcat从单独的线程处理每个请求,因此,每当同时发出两个请求时,就会出现问题。

You have a number of options: 您有多种选择:

  • instantiate the SimpleDateFormat on each call of the method (rather than making it static ) 在方法的每次调用上实例化SimpleDateFormat (而不是使其static
  • if you need the format more than once per thread, use a ThreadLocal to store it. 如果每个线程需要多次使用该格式,请使用ThreadLocal进行存储。
  • alternatively use joda-time, where DateTimeFormat is thread-safe. 或者使用joda-time,其中DateTimeFormat是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM