This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
K Brahma
Greenhorn
Posts: 4
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic