Non Managed Resources (1)

Problem:

  • Application need to access Non-Managed Resource, for instance a plain TCP Connection
  • Resources are heavily used
  • To producing Resource, for instance to establish a TCP Connection and application-specific initialization, is time consuming
  • Operations on Resources are stateless and nontransactional

Resources are, for example, connections, any connections like database connections, JMS connections, plain TCP connections. Resources are usually produced by Resource Managers. For example database connections are established by JDBC driver or DataSource. Resource Managers are drivers, factories, data sources of any kind. A plain TCP connection does not use Resource Manager. Usually Resources are not thread safe, it means that two different threads should not use one Resource simultaneously.

Resource Managers and Resources are Managed or Non-Managed. Managed Resource Managers are administered by an application server, are created and destroyed by a server, a server can manage transactions, security and pools of Resources for reuse purposes. Non-Managed Resource Managers and Resources are managed by an application, an application provider is responsible for them.

Solution:

Use Stateless Session EJB to wrap connections:

@Stateless
public class ConnectionHolder {
   private Connection conn;
   @PostConstruct
   private void init(){
      //create connection
      conn = ...
   }
   //method performing basic operation
   public String sendRequest(String request){
      //basic resource-specific operation
      String response = conn.send(request);
      return response;
   }
   @PreDestroy
   private void destroy(){
      //close connection
      conn.close();
   }
}

And adequate client code:

   //reference EJB
   @EJB private ConnectionHolder conn;
   ...
   public void doSmth(){
      ...
      conn.sendRequest(someData);
      ...
   }

There can be more than one basic operation. Operations can be composed of sequence of connection invocations, for example: first request is send via a TCP Connection, two response is received.

Benefits:

  • Reuse and pooling: creating and destroying beans means creating and closing connections, one bean – one connection, EJB Container maintains beans pool accordingly to load, number of opened connections is limited
  • Physical operations are separated from an application
  • Business logic is separated from ConnectionHolder

Modification:

If ConnectionHolder has remote interface, it can be used by many applications. And ConnectionHolder EJB can be used as integration mechanism, perform access control.

Bean can check if its connection is alive and restart it if necessary:

@Stateless
public class ConnectionHolder {
   ...
   //method performing basic operation
   public String sendRequest(String request){
      if ( ! conn.isAlive() ){
         //establishe new connection
         conn = ...
      }
      //basic resource-specific operation
      String response = conn.send(request);
      return response;
   }
   ...
}

Alternatively, connection can be pinged with fast and simple request. Moreover, ping can be performed when last access time is old enough. But bean must maintain last access time.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: