Monday, September 27, 2010

Resource Handling

Resource Handling
In Java, Most of the time we open some resource and after using we forget to close the resource and that make a lots of open connection for the resouse if we are not using a good ConnectionPool for resource. Still if we are using the connectino pool, then also we need to release the resopuse after use, so other can use that resource for their operation.
here is sample framework which basically takes care of these part in your java program.

Here I have two classes, one is TransactionGuard and other is Command interface.

TransactionGuard.java

/**
* TransactionGuard is make sure you release the particular resource after use.
* By using any resource, it will make sure that you are closing the resource
* after using. To use this one need to implement getConnection and
* releaseConnection method based on the need.
*
* @author Virendra.Balaut
*
* @param
* an element/resource for which you want to implement
* TransactionGuard
*/
public abstract class TransactionGuard
{
/**
* This is the main execute method which takes care of getting a connection of T type,
* do some operation and after use close the connection.
*
* @param command
* @throws Exception
*/
public void execute(Command command) throws Exception
{
T connection = null;
try
{
connection = getConnection();
command.execute(connection);
}
finally
{
releaseConnection(connection);
}

}

/**
* Abstract method to get Connection for a dedicated Type.
*
* @return
*/
protected abstract T getConnection() throws Exception;

/**
* Abstract method to release connection for a dedicated type.
*
* @param connection
*/
protected abstract void releaseConnection(T connection) throws Exception;
}


Command.java


public abstract class Command
{
public abstract void execute();
}

Using these framework will ensure that resouce will automatically closed after use.

Sample Example :-

Here I have sample program which will do database operation. So I need to open a database connection and after use it I need to release that connection.

new TransactionGuard()
{

@Override
protected Connection getConnection() throws Exception
{
Connection connection = createConnection();
return connection;
}

@Override
protected void releaseConnection(Connection connection)
throws Exception
{
if(connection != null) {
connection.close();
}

}
}.execute(new Command()
{

public void execute(Connection connection) throws Exception
{
// Do some operation with connection.

}
});


Let me know your suggestions/views.

1 comment:

Ketan said...

While the functionality is useful, I think you should change the Terminology. Since this is for any generic resource, instead of "Connection" you can use "Resource" (i.e. getResource(), releaseResource() etc.)

Using Connection gives the impression that it is not generic.

Also, why is this component named "Transaction" Guard? Is there anything specific to transactions here? It seems it is basically a way to enforce resource cleanup - the word "Transaction" seems confusing...

Just a couple of thoughts...