Search code examples
javaejbjndidisconnect

Detecting EJB Datasource client disconnect


The server I'm using is GlassFish, but this problem also occurs on Weblogic as well.

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).

The database queries opened by the client machine are never closed (because the client terminated) and this causes deadlocks and other issues.

Is there any way to detect this dead transaction and reclaim all the resources?

Pseudocode example below

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();

Solution

  • IIRC this is can't be easily handled. It depends on the database you use what you need to do use the DB administration to find the hanging transactions.

    To have a more relyable environment you should create a server side bean for this, here it is more unlikely that the process died or loose connection somewhere during the transaction.