4

I am trying to spawn a thread for just 1 method. Im getting an IllegalStateException (see below). What this method does it accept a Connection to a database and the database name, and it will generate XML from it. (That part works I am just trying to make it go faster with a new thread because I have multiple XML files to create.

    Thread table = new Thread(new Runnable() {
    public void run() {
    try {
     System.out.println("starting");
     tableXml(tableConn, dbName);
     System.out.println("ending");
     }
     catch (Exception e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
      }
  }
});

    table.start();

Exception:

java.lang.IllegalStateException: Current state = RESET, new state = FLUSHED
    at java.nio.charset.CharsetEncoder.throwIllegalStateException(CharsetEncoder.java:951)
    at java.nio.charset.CharsetEncoder.flush(CharsetEncoder.java:640)
    at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:769)
    at com.informix.lang.JavaToIfxType.doConversion(JavaToIfxType.java:841)
    at com.informix.lang.JavaToIfxType.JavaToIfxChar(JavaToIfxType.java:145)
    at com.informix.jdbc.IfxVarChar.toString(IfxVarChar.java:247)
    at com.informix.jdbc.IfxResultSet.getString(IfxResultSet.java:742)
    at com.informix.jdbc.IfxResultSet.getString(IfxResultSet.java:785)
    at org.apache.commons.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:225)
    at com.test.ex.ExportTask$1.run(ExportTask.java:151)
    at java.lang.Thread.run(Thread.java:662)

The line of code that is causing the exception is a resultSet.executeQuery();

So the question is: what am I doing wrong?

Thanks for your help, let me know if you need information

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
RMT
  • 7,040
  • 4
  • 25
  • 37

3 Answers3

8

It seems that the problem is because your code is not thread safe. Try giving each new thread that is spawned its own connection instead of sharing a reference of a single connection between all threads in your application.

John Kane
  • 4,383
  • 1
  • 24
  • 42
4

The exception itself has nothing to do with the way you're firing up a new thread. You're "start-thread-call-method" code is just fine.

The exception is CharsetEncoder related. If I were you, I would google for something like IllegalStateException CharsetEncoder and possibly thread safety.

It is important however, that Whatever classes / framework you're using concurrently are thread safe.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Then the classes / frameworks you use concurrently probably aren't thread safe. Read up on the documentation. – aioobe Jun 15 '11 at 12:12
0

Are you sure the main thread doesn't reset/close the connection while the spawned thread tries using it?

If you're in a Java EE environment (or similar, like Spring), the connection is often linked to the current transaction, which is itself linked to the current thread. So when the transaction ends, the connection is given back to the pool of available connections, but your spawned thread is still trying to use it.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I am making sure that the connection is not being closed until everything has been finished. – RMT Jun 15 '11 at 12:18