aspose file tools*
The moose likes JBoss/WildFly and the fly likes EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly : JBoss 6 : EJB 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly : JBoss 6 : EJB 3" Watch "EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly : JBoss 6 : EJB 3" New topic
Author

EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly : JBoss 6 : EJB 3

K Brahma
Greenhorn

Joined: Aug 18, 2010
Posts: 4

Hi,

I was experimenting with BMT and StatefulBean in EJB 3 in JBoss 6 server. I am using MySQL as database. The SFSB has two methods. Both the methods are inserting some data in Database. Following is the code for the SFSB:

@Remote(StatefulBMTRemote.class)
@Stateful(name="StatefulBeanBMT")
@Resource(name="jdbc/MySqlDS",
mappedName="java:/MySqlDS", type=javax.sql.DataSource.class)
@TransactionManagement(TransactionManagementType.BEAN)
public class StatefulBMTBean
{

@Resource(name="jdbc/MySqlDS")
private DataSource dbSrc;

private Connection con ;

private Connection con ;


private String userId;
private String password;

private String userName;
private String address;

@Resource private UserTransaction utx;

@PostConstruct
@PostActivate
void initialize()
{
try {
con = dbSrc.getConnection();
} catch (SQLException e) {

}

}

@PreDestroy
@PrePassivate
void release()
{
if(con != null)
{
try {
con.close();
} catch (SQLException e) {

}
con = null;
}

}

public void addUserCredential(String userId, String pwd)
{
try
{
utx.begin();
} catch (NotSupportedException e)
{

} catch (SystemException e)
{

}
this.userId = userId;
this.password = pwd;
try {

Statement stmt = con.createStatement();
String sql = "insert into users (USER_ID, USER_PASSWORD) " +
"values ('" + userId + "','" + password
+ "')";
stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
try
{
utx.rollback();
} catch (IllegalStateException e1)
{

} catch (SecurityException e1)
{

} catch (SystemException e1)
{

}
}
}

public void addUserDetail(String userName, String address)
{
this.userName = userName;
this.address = address;
try {
Statement stmt = con.createStatement();
String sql = "select max(detail_id) from user_detail";
ResultSet rs = stmt.executeQuery(sql);
int maxId = 0;
if(rs.next())
{
maxId = rs.getInt(1);
}
sql = "insert into user_detail values(" + (++maxId) + ",'" + this.userName
+ "','" + this.address + "')";
stmt.executeUpdate(sql);
rs.close();
stmt.close();

} catch (SQLException e) {
try
{
utx.rollback();
} catch (IllegalStateException e1)
{
} catch (SecurityException e1)
{
} catch (SystemException e1)
{
}
}

}


@Remove
public void cancelAdd()
{
userId = null;
userName = null;
password = null;
address = null;
try
{
utx.rollback();
} catch (IllegalStateException e1)
{
} catch (SecurityException e1)
{
} catch (SystemException e1)
{
}
}

@Remove
public void commit()
{

try
{
utx.commit();

} catch (SecurityException e)
{
} catch (IllegalStateException e)
{
} catch (RollbackException e)
{
} catch (HeuristicMixedException e)
{
} catch (HeuristicRollbackException e)
{
} catch (SystemException e)
{
}

}






The MySQL ds file content is as follows:

<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root123</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>


I have created table with InnoDB Engine.

From a standalone java client when I am accessing the SFSB I founded that even if I call cancelAdd(), The rollack is not taking place and data is staying in Database. After Stacktrace enabling I found no exception either.


This is driving me mad Because This is one of the advantages of using BMT in SFSB.

Please help.

Thanks in Advance

Thanks in advance.
Cheers
KB
-----------------------
SCJP6(95%), OCPJBCD(100%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly : JBoss 6 : EJB 3