我有以下情况:
2名有会话的swingworkers从数据库收集数据。现在我要取消第二个swingworker。我做swingworker.cancel(true),在swingworker的done()方法中,我用session.close()关闭会话。
在我完成之后,我认为swingworker有一个sql语句,他正在执行但是他被取消并且会话被关闭所以发生以下执行:
java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.getResourceRegistry(ConnectionProxyHandler.java:104)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.getResourceRegistry(AbstractStatementProxyHandler.java:73)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.wrapIfNecessary(AbstractStatementProxyHandler.java:150)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:123)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy9.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.scroll(Loader.java:2510)
at org.hibernate.loader.custom.CustomLoader.scroll(CustomLoader.java:337)
at org.hibernate.internal.SessionImpl.scrollCustomQuery(SessionImpl.java:1760)
at org.hibernate.internal.AbstractSessionImpl.scroll(AbstractSessionImpl.java:232)
at org.hibernate.internal.SQLQueryImpl.scroll(SQLQueryImpl.java:182)
at de.mudisar.dataloader.SecondarySwingWorker.doInBackground(SecondarySwingWorker.java:71)
at de.mudisar.dataloader.SecondarySwingWorker.doInBackground(SecondarySwingWorker.java:32)
at javax.swing.SwingWorker$1.call(SwingWorker.java:277)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
我该怎么做才能解决问题。如果我取消了swingworker,我希望所有sql操作或其他东西都应该被取消,然后关闭会话。
//编辑:
这是我的swingworker的代码。这个摇摆工作者有自己的会议:
@Override
protected Void doInBackground() throws Exception {
doMappings();
fetchSQL();
}
private void fetchSQL(){
for(String str : map.keySet()){
//build sql;
SQLQuery q = sess.createSQLQuery(query).addEntity("ins", ALL_RSC_DATA.class);
q.setFetchSize(Integer.valueOf(1000));
q.setReadOnly(true);
ScrollableResults results = q.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
//processing
}
}
}
这是我的另一个处理用户输入的类的动作(取消swingworker):
public Action cancelPrimarySwingWorker = new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
if(psw != null && !psw.isDone()){
closeDBConnection(psw.getSession());
psw.cancel(true);
psw = null;
restoreData();
}
}
};
有关完整信息,请参阅closeDBConnection()的代码:
@SuppressWarnings("deprecation")
public static void closeDBConnection(Session session) {
if(session != null){
try {
//session.connection().createStatement().execute("SHUTDOWN");
if(session.isOpen()){
session.cancelQuery();
session.clear();
session.close();
}
session = null;
}
catch (Exception sqle) {
logging(sqle);
System.exit(1);
}
}
}
答案 0 :(得分:1)
public void actionPerformed(ActionEvent arg0) {
if(psw != null && !psw.isDone()){
closeDBConnection(psw.getSession());
psw.cancel(true);
psw = null;
restoreData();
}
这肯定会让我觉得你在读取器线程仍在运行时关闭数据库连接,这可能会导致你所看到的正是这样。您需要对事物进行重组,以便读取器线程响应取消并在finally块中关闭连接本身。