并发数据库访问提供 IllegalStateException [英] Concurrent database access giving IllegalStateException
问题描述
我正在尝试为一种方法生成一个线程.我收到一个 IllegalStateException
(见下文).这个方法是做什么的,它接受一个到数据库的连接和数据库名称,它会从中生成 XML.(这部分有效,我只是想通过一个新线程让它运行得更快,因为我要创建多个 XML 文件.
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();
异常:
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)
导致异常的代码行是 resultSet.executeQuery();
所以问题是:我做错了什么?
So the question is: what am I doing wrong?
感谢您的帮助,如果您需要信息,请告诉我
Thanks for your help, let me know if you need information
推荐答案
问题似乎是因为您的代码不是线程安全的.尝试为产生的每个新线程提供自己的连接,而不是在应用程序中的所有线程之间共享单个连接的引用.
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.
这篇关于并发数据库访问提供 IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!