我有以下情况:
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 代码。这个 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);
}
}
}