Hi,
I am planning to appear in SCBCD in next month.
I was experimenting with BMT and StatefulBean in
EJB 3 using
JBoss 6 server along with 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.
Keep the transaction open across bean methods is something that can be done through SFSB only and though as the most promising benefit you can never achieve through SLSB. So why is this happening so I am absolutely clueless about.
Please help.
Thanks in advance.
KB