I decided to give this a try on my local setup. I used MySQL-5.0.20 with JBoss-4.2.2. I
wrote a similar EJB as yours to try this out:
/**
| *
| */
| package org.myapp.ejb.impl;
|
| import java.io.BufferedReader;
| import java.io.InputStreamReader;
| import java.sql.SQLException;
| import java.sql.Statement;
| import java.util.HashSet;
| import java.util.List;
| import java.util.Set;
|
| import javax.annotation.Resource;
| import javax.ejb.Remote;
| import javax.ejb.SessionContext;
| import javax.ejb.Stateless;
| import javax.ejb.TransactionAttribute;
| import javax.ejb.TransactionAttributeType;
| import javax.naming.InitialContext;
| import javax.persistence.EntityManager;
| import javax.persistence.PersistenceContext;
| import javax.persistence.Query;
|
| import org.jboss.annotation.ejb.RemoteBinding;
| import org.myapp.ejb.UserManager;
| import org.myapp.entity.Account;
| import org.myapp.entity.User;
| import org.myapp.exception.ApplicationException;
|
| /**
| * @author Jaikiran Pai
| * @since
| */
| @Stateless
| @Remote ({UserManager.class})
| @RemoteBinding (jndiBinding = "RemoteUserManagerBean")
| public class UserManagerBean implements UserManager {
|
| @PersistenceContext
| private EntityManager entityManager;
|
| @Resource
| private SessionContext sessionContext;
|
|
|
| public void test(String userName)
| {
| java.sql.Connection conn = null;
| try {
| javax.sql.DataSource ds = (javax.sql.DataSource) new
InitialContext().lookup("java:/EJB3PersistenceDS");
| conn = ds.getConnection();
|
| System.out.println("IS AUTOCOMMIT: " + (conn.getAutoCommit()));
|
| Statement statement = conn.createStatement();
|
| statement.execute("INSERT INTO USER (NAME) VALUES('" + userName +
"')");
| System.out.println("Inserted user " + userName);
| System.out.println("Session Context is " + sessionContext);
|
| System.out.println("Called setRollbackOnly");
| System.out.println("Is it set = " + sessionContext.getRollbackOnly());
|
|
| if (true) {
| System.out.println("Throwing exception");
| throw new RuntimeException("Intentional exception");
| }
| statement.close();
|
|
|
| } catch (RuntimeException re) {
| throw re;
| } catch (Exception ex) {
| System.out.println(ex);
| ex.printStackTrace();
| } finally {
| if (conn != null) {
| try {
| conn.close();
| } catch (SQLException e) {
| // TODO Auto-generated catch block
| e.printStackTrace();
| }
| }
| }
| }
|
| }
|
And yes, i too see the same behaviour. The insert is not rolled back. I then switched my
database to MS SQLServer to see if this is specific to MySQL and observed that the insert
was rolled back on that DB server. So this looks like specific to MySQL.
I have read in many places that MySQL globally set autocommit to true. I tried various
ways to disable this, including what's been mentioned at
http://www.oreillynet.com/databases/blog/2007/02/mysql_transactions_and_a..., but
havent been able to get this working. If you can somehow disable the autocommit on the
MySQL server then i guess you should be able to get this working.
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4120880#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...