I have the following situation:
2 swingworkers with session which collect data from the database. Now I want to cancel the second swingworker. I do swingworker.cancel(true) and in the done() method from the swingworker I close the session with session.close().
After I have done that I think the swingworker has one sql statement which he is executing but he is cancelled and the session is closed so the following execption occures:
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)
What can i do to solve the problem. If i cancel the swingworker i want that all sql operations or other things should be cancelled and then close the session.
//EDIT:
Here is my code from my swingworker. This swingworker has its own session:
@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
}
}
}
And this is my action (for cancel the swingworker) from my other class which handles the user inputs:
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();
}
}
};
For the complete information here my code for 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);
}
}
}
public void actionPerformed(ActionEvent arg0) {
if(psw != null && !psw.isDone()){
closeDBConnection(psw.getSession());
psw.cancel(true);
psw = null;
restoreData();
}
This sure looks to me like you're closing the db connection while the reader thread is still running, which would probably result in exactly what you're seeing. You need to restructure things so the reader thread responds to the cancel and closes the connection itself in a finally block.