Skip to main content

Wierd threading issue

2 replies [Last post]
sproketboy
Offline
Joined: 2006-08-07
Points: 0

Hi All,
In a part of our code I want to create a new thread to finish off some processing work so I can respond back to the client sooner. When I use the new Thread directly in my method it all works fine. But if I move the thread call to into a private method then sometimes the new connection is closed before I finish. any ideas?

// this form works fine. I get a new connection for the new thread.

final InterpValue iv = interpValue;
final InterpValidatorContext vc = validatorContext;
final int bsic = beforeSaveInterpCount;
new Thread("triggerPostUpdateEvents") {
   public void run() {
      try {
         Connection con = DataSourceFactory.getFactory().getConnection();
         IBusinessContextInfo bci = new DisposableContextInfo(con, contextInfo, false);
         triggerPostUpdateEvents(iv, vc, bsic, bci);
         bci.cleanup();
      } catch (Exception e) {
         log.error("Error when triggering events", e);
      }
   }
}.start();

// this form fails sometimes

launchPostUpdateEvents(beforeSaveInterpCount, interpValue, validatorContext, contextInfo);

...

...

    private void launchPostUpdateEvents(final int beforeSaveInterpCount, final InterpValue interpValue, final InterpValidatorContext validatorContext, final IBusinessContextInfo contextInfo) {
        new Thread("triggerPostUpdateEvents") {
            public void run() {
                try {
                    Connection con = DataSourceFactory.getFactory().getConnection();
                    IBusinessContextInfo bci = new DisposableContextInfo(con, contextInfo, false);
                    triggerPostUpdateEvents(interpValue, validatorContext, beforeSaveInterpCount, bci);
                    bci.cleanup();
                } catch (Exception e) {
                    log.error("Error when triggering events", e);
                }
            }
        }.start();
    }

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tianwang
Offline
Joined: 2008-06-07
Points: 0

I don't have much clues here.
Is your connection being managed by a container? Maybe DataSourceFactory.getFactory().getConnection() is returning a connection whose transaction is managed by some container.
At first, your method probably does alot more other work after starting the other thread, so there would have been enough time for your other thread to finish up its work before the connection/transaction is killed. Maybe not the case after the changes.
Shrugs

sproketboy
Offline
Joined: 2006-08-07
Points: 0

Thanks. I figured it out. The method made use of the original ContexnInfo object where it should have used the new local one.