title: Detecting EJB Datasource client disconnect
body: <p>The server I'm using is GlassFish, but this problem also occurs on Weblogic as well.</p>
  
  <p>My client machine begins a transaction, looks up several EJBs from the server, and begins some work which takes a while to complete. During the process, the client machine dies (let's say a powercut).</p>
  
  <p>The database queries opened by the client machine are never closed (because the client terminated) and this causes deadlocks and other issues.</p>
  
  <p>Is there any way to detect this dead transaction and reclaim all the resources?</p>
  
  <p>Pseudocode example below</p>
  
  <pre><code>transaction.begin();
  
  MyBean bean = (MyBean)ctx.lookup("MyBean");
  
  bean.doComplicatedWorkPart1();
  
  bean.writeResultsToTheDatabase();
  
  // Client dies during this method (powercut), so the transaction
  // is simply left hanging.
  bean.doComplicatedWorkPart2();
  
  bean.writeResultsToTheDatabase();
  
  transaction.commit();
  </code></pre>