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

EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly

K Brahma
Greenhorn

Joined: Aug 18, 2010
Posts: 4

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

Thanks in advance.
Cheers
KB
-----------------------
SCJP6(95%), OCPJBCD(100%)
Jaroslaw Szczepankiewicz
Greenhorn

Joined: Sep 07, 2011
Posts: 2
make sure to disable autocommit, mysql is not good type of database for learning / adherence to standards and might have enabled autocommit.


SCJP 6 (81%), OCE-EJBD 6 (90%)
K Brahma
Greenhorn

Joined: Aug 18, 2010
Posts: 4

Where exactly I should set the autocommit false for MySQL. My guess is I need to set this property at data source level, meaning in the mysql-ds file? Or shall I set this on the Connection after getting it from Datasource?
Romeo Son
Ranch Hand

Joined: Mar 12, 2007
Posts: 92

Are you sure you don't get any exceptions on the client? Take a look:

http://www.coderanch.com/t/491473/java-EJB-SCBCD/certification/Remove-method-inside-transaction-stateful


SCJP 1.4, SCJD, OCE EJBD 6
K Brahma
Greenhorn

Joined: Aug 18, 2010
Posts: 4

I am not getting any exception on client as well as at the server end.

Jaroslaw,

What I don't get is why I should bother about commit.

When I am using CMT and doing some operation distributed over two different SLSB with transaction attribute REQUIRED, if I get any exception at the second bean and mark the transaction for rollback the entire operation gets rolled back by the container and even if there is a successful database DML operation in the first bean that will be rolled back too.

For BMT only difference is the application is starting the transaction. So my expectation was that whatever database operation I am doing over different methods of a SFSB, all will be committed only if I commit transaction.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly