Parallel database access providing IllegalStateException

I am trying to create a thread for only one method. Im getting an IllegalStateException (see below). In this method, it accepts a connection to the database and the database name, and it will generate XML from it. (This part works, I'm just trying to make it faster with a new stream, because I have several 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(); 

An 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 resultSet.executeQuery(); exception is resultSet.executeQuery();

So the question is: what am I doing wrong?

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

+4
source share
3 answers

The problem seems to be that your code is not thread safe. Try to give each new thread that spawned its own connection, instead of sharing the link of one connection between all the threads in your application.

+8
source

The exception has nothing to do with how you start a new thread. The code "start-thread-call-method" is very good.

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

It is important, however, that any classes / frameworks that you use at the same time are thread safe .

+4
source

Are you sure that the main thread does not reset / close the connection while the spawned thread is trying to use it?

If you are in a Java EE environment (or similar, such as Spring), the connection is often associated with the current transaction, which itself is associated with the current thread. Therefore, when the transaction completes, the connection returns to the pool of available connections, but your spawned thread is still trying to use it.

0
source

All Articles