Dealing With Multiple Data Sources in EJB3 2

There are some situations in which we need to take a connection from other data sources different from that which our EJB is using.  like:


public void check(){
  Connection con = null;
  try {
    Internal internal = entityManager.find(Internal.class, 20);
    System.out.println(internalIP);

    // getting another connection from other data source i.e. "java:jboss/jdbc/ejb/sourceTwo"
    con = getDataSource().getConnection();
    Statement statement = con.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from Interna where id = 20");

    while (resultSet.next()) {
     System.out.println((String) resultSet.getObject(2));
    }
  } finally {
    con.close();
  }
}


 public static DataSource getDataSource() throws NamingException {
   Context ctx = new InitialContext();
   return (javax.sql.DataSource) ctx.lookup( "java:jboss/jdbc/ejb/sourceTwo");
 }

Check is the method defined in an EJB, so it is in transactional scope and in this we are trying to get a connection from another data source. Now this will create a problem and at runtime and we will get the below exception:


WARN  [com.arjuna.ats.arjuna] (http-/172.16.3.101:8280-1) ARJUNA012140: Adding multiple last resources is disallowed. Trying to add LastResourceRecord(XAOn
ePhaseResource(LocalXAResourceImpl@60977b45[connectionListener=791346f4 connectionManager=7ac4e3b8 warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff0a6362da:-4ce0fc8:528b4b45:18, node_name=1, branch_uid=0:ffff0a6362da:-4ce0fc8:528b4b45:1d, subordinatenodename=null, eis_name=java:jboss/jdbc/ejb/sourceTwo > productName=Microsoft SQL Server productVersion=10.00.2573 jndiName=java:jboss/jdbc/ejb/sourceTwo])), but already have LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@5600774d[connectionListener=3b1ca6a4 connectionManager=3735357a warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff0a6362da:-4ce0fc8:528b4b45:18, node_name=1, branch_uid=0:ffff0a6362da:-4ce0fc8:528b4b45:1c, subordinatenodename=null, eis_name=java:jboss/jdbc/ejb/sourceOne > productName=Microsoft SQL Server productVersion=10.00.2573 jndiName=java:jboss/jdbc/ejb/sourceOne]))

So in simple words it is saying that ” Adding multiple last resources is disallowed. We are trying to add LastResourceRecord java:jboss/jdbc/ejb/sourceTwo but already have LastResourceRecord java:jboss/jdbc/ejb/sourceOne”.

Now how to deal this type of situation?

By extracting the code of making new connection outside in an new method and then annotate it with @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) on the EJB method.

The reason why we are doing this is ‘There is transaction going on that is using a connection from datasource named ‘ABC’, but now we need to perform some other operation in other transaction so we need a connection from other datasource named ‘DEF’, that is why we need to suspend the first trasaction by saying NOT_SUPPORTED and do the necessary operation on the new connection and then again resume the same old transaction’

2 thoughts on “Dealing With Multiple Data Sources in EJB3

  1. Reply Gaurav Walia Feb 6,2014 3:27 pm

    Just came across a similar issue in my project. Thumbs up for the perfect solution. Works great with ‘NOT_SUPPORTED’ annotation.

  2. Reply poker online indonesia Aug 4,2015 3:33 pm

    Everything is very open with a clear description of the challenges.
    It was definitely informative. Your site is very helpful. Thanks
    for sharing!

Leave a Reply