aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes An illegal attempt to commit a one phase capable resource with existing two phase Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "An illegal attempt to commit a one phase capable resource with existing two phase" Watch "An illegal attempt to commit a one phase capable resource with existing two phase" New topic
Author

An illegal attempt to commit a one phase capable resource with existing two phase

Poonam Thawani
Greenhorn

Joined: Oct 11, 2005
Posts: 13
Hello ranchers,
Urgent info needed:-
I am facing a problem with mesage driven bean.
WTRN0063E: An illegal attempt to commit a one phase capable resource with existing two phase capable resources has occurred.
Code in onMessageBean of MDB is:-
It reads from MQ using JMS and inserts into the database.
Any inputs will be of great help.
Code:-

package com.message;
import javax.ejb.*;
import javax.naming.*;
import java.io.File;
import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.ibm.websphere.ce.cm.StaleConnectionException;


/**
* Bean implementation class for Enterprise Bean: SimpleMessage
*/
public class SimpleMessageBean
implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
private javax.ejb.MessageDrivenContext fMessageDrivenCtx;
private DataSource itDS;
private Connection con;





/**
* getMessageDrivenContext
*/
public javax.ejb.MessageDrivenContext getMessageDrivenContext() {
return fMessageDrivenCtx;
}
/**
* setMessageDrivenContext
*/
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx) {
fMessageDrivenCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() {
System.out.println(" In SimpleMessageBean ejbCreate");
}
/**
* onMessage
*/
public void onMessage(javax.jms.Message msg) {

try
{

System.out.println("In On Message");

System.out.println("Message Received Poonam"+msg);

System.out.println("Message id is "+msg.getJMSMessageID());
String strMessageId=new String(msg.getJMSMessageID());
System.out.println("Message id new String strMessageId is "+ strMessageId);
strMessageId=msg.getJMSMessageID().getBytes().toString();
System.out.println("Message id strMessageId bytes to string"+new String (strMessageId.getBytes()));
TextMessage txtmsg=(TextMessage)msg;
System.out.println("This is text message"+txtmsg.getText());
dbConfiguration(txtmsg.getJMSMessageID(),txtmsg.getJMSCorrelationID(),"",txtmsg.getText());
System.out.println("Leaving onMessage method");






}
catch(Exception e){
e.printStackTrace();
}

}
/**
* @param strMessageId
* @param strCorrelationId
* @param strUserId
* @param strGroupId
* @param strsequenceNo
* @param strResponseXML
*/
private void dbConfiguration(String strMessageId, String strCorrelationId, String strUserId, String strResponseXML) {
// TODO Auto-generated method stub
//String url = "jdbc:inetpool:inetdae7a:USTRESCHIDEV1:1433";
final String strITDs = "jdbc/FX";

String strsql2="INSERT INTO it_adaptiv_response_detail (MessageId,CorrelationId,UserId,ResponseXML) "+
"values ('"+strMessageId+"','"+strCorrelationId+"','"+strUserId+"','"+strResponseXML+"')";
//String strSQL="SELECT * FROM it_adaptiv_response_detail";
System.out.print("Within dbConfiguration");
try {

itDS=getDataSource(strITDs);

System.out.println("DataSource Obtained"+itDS.toString());
System.out.println("Getting connection");
con=getDBConnection();
Statement stmt=con.prepareStatement(strsql2);
System.out.println("Before insert "+strsql2);
//con.setAutoCommit(false);
boolean bl=stmt.execute(strsql2);
System.out.println("After insert");
//con.commit();
stmt.close();
con.close();

//con.setAutoCommit(true);
//stmt.execute(strSQL);
/*
Connection con=itDS.getConnection();
Statement stmt = con.createStatement();
stmt.execute(strsql);
System.out.print(con.getAutoCommit());
System.out.print("After stmt.execute "+strsql);
con.setAutoCommit(true);
stmt.close();
con.close();*/
//DBOperations db=new DBOperations("IT");
//db.runQuery(strsql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("Leaving dbConfiguration");

}
// TODO Auto-generated method stub

/*private DataSource getDataSource(String jndiName) throws Exception {

InitialContext initCtx = new InitialContext();
DataSource ds;

try {
System.out.println("JNDI NAME "+jndiName);
//Context envCtx = (Context) initCtx.lookup("java:comp/env");
//ds = (DataSource) initCtx.lookup(jndiName);
ds = (DataSource) initCtx.lookup(jndiName);
System.out.println("Datasource "+ds);

}
catch (Exception e) {
ds = (DataSource) initCtx.lookup(jndiName);
}




System.out.println("Leaving getDataSource");
return ds;

}*/

private DataSource getDataSource(String jndiName) throws Exception {
String _method = "getDataSource ( )";
System.out.println("Entering " + _method);


DataSource ds;
try {

Context initCtx = new InitialContext();


try {
System.out.println("In try msg driven bean");
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(jndiName);
System.out.println("In try msg driven bean After datasource lookup");
}
catch (Exception e) {
ds = (DataSource) initCtx.lookup(jndiName);
System.out.println("In exception MDB");
}

}

catch (Exception ex) {
ex.printStackTrace();
throw new Exception("Unable to load JDBC driver: "
+ jndiName);

}

System.out.println("Leaving " + _method);

return ds;
}
/**
* ejbRemove*/
public void ejbRemove() {
System.out.println(" In SimpleMessageBean ejbRemove");
}

/**
* Get a connection. Handle the IBM Websphere StaleConnectionException.
* If the db has been restarted, the connections in WebSphere's pool may
* be invalid. There is no way to know until they are used, but at that
* point a StaleConnectionException is thrown and WAS refreshes the pool.
* The next attempt to get a connection should get a good one.
*
* @return Connection
* @throws SQLException If there is problem getting connection.
*/
private Connection getDBConnection() throws SQLException {

String _method = "getDBConnection ( )";
System.out.println("Entering " + _method);

boolean retry = false;
int numOfRetries = 0;


Connection con = null;
Statement stmt = null;
//Dummy SQL - need to try and use conn to get StaleConnection
String sql = "SELECT count(*) FROM it_EOD_info";
do {
retry = false;
try {
con = itDS.getConnection();
stmt = con.createStatement();
stmt.executeQuery(sql);
System.out.println("con.getAutoCommit()=[" + con.getAutoCommit() + "]");
System.out.println("con.getTransactionIsolation=[" + con.getTransactionIsolation() + "]");
// con.setAutoCommit(true);
// if (Level.DEBUG == log.getLevel()) {
//String tranCount = getTrancount(con);
// log.debug("[@@TRANCOUNT=" + tranCount + "]");
// }
}
catch (StaleConnectionException sce) {
if (numOfRetries < 2) {
retry = true;
numOfRetries++;
System.out.println("Got stale connection " + numOfRetries + " time(s) :" + sce);
}
else {
System.out.println("Couldn't get connection :" + sce);
throw new SQLException("Failing - got stale connection three times");
}
}
finally {
closeStatment(stmt);
}
} while (retry);

System.out.println("Leaving " + _method);

return con;
}

private String getTrancount(Connection con) {

String _method = "getTrancount ( )";
System.out.println("Entering " + _method);

Statement stmt = null;
ResultSet rs = null;
String retVal = "Z";
try {
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT @@TRANCOUNT");
if (rs.next()) {
retVal = rs.getString(1);
}
else {
retVal = "X";
}
}
catch (SQLException e) {
System.out.println("Exception " + e);
}

System.out.println("Leaving " + _method);

return retVal;
}



private void closeStatment(Statement statement) {
try {
if (statement != null) {
statement.close();
}
}
catch (SQLException ignore) {
ignore.printStackTrace();
}
}

}
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
Could you let me know the driver/datasource classes used. The problem is occuring because you are trying to do a 2 Phase Commit transactions using non-XA drivers. Check if your drivers for messaging and database are XA compliant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An illegal attempt to commit a one phase capable resource with existing two phase