1 25 package org.objectweb.easybeans.tests.common.ejbs.base.transaction; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 30 import javax.annotation.Resource; 31 import javax.ejb.SessionContext ; 32 import javax.sql.DataSource ; 33 import javax.transaction.UserTransaction ; 34 35 import org.objectweb.easybeans.tests.common.db.TableManager; 36 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00; 37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.SLSBTransactionMisc00; 38 import org.objectweb.easybeans.tests.common.exception.TransactionException; 39 import org.objectweb.easybeans.tests.common.helper.DBHelper; 40 import org.objectweb.easybeans.tests.common.helper.EJBHelper; 41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper; 42 43 48 public class ContainerTransaction implements ItfContainerTransaction { 49 50 53 @Resource 54 private SessionContext ctx; 55 56 61 protected void insertTable(final String dbName) throws Exception { 62 TableManager tableManager = new TableManager(dbName); 63 tableManager.insertTable(TABLE); 64 } 65 66 72 public void insertCorrectTableInBothDB(final String db1, final String db2) throws Exception { 73 insertTable(db1); 74 insertTable(db2); 75 76 } 77 78 83 private void makeSQLError(final String db1) throws Exception { 84 Connection connection = null; 85 DataSource ds = DBHelper.getDataSource(db1); 86 try { 87 connection = ds.getConnection(); 89 PreparedStatement stmUpdate = null; 90 try { 92 stmUpdate = connection.prepareStatement("CREATE TABLE error("); 93 stmUpdate.executeUpdate(); 94 } finally { 95 if (stmUpdate != null) { 96 stmUpdate.close(); 97 } 98 } 99 } finally { 100 if (connection != null) { 101 connection.close(); 102 } 103 } 104 } 105 106 113 public void insertCorrectFirstErrorSecond(final String db1, final String db2) throws Exception { 114 insertTable(db1); 116 makeSQLError(db2); 118 119 } 120 121 128 public void setRollbackOnly(final String dbName1, final String dbName2) throws TransactionException, Exception { 129 insertTable(dbName1); 130 insertTable(dbName2); 131 try { 132 ctx.setRollbackOnly(); 133 } catch (IllegalStateException e) { 134 throw new TransactionException("There was an exception in the getRollbackOnly", e); 135 } 136 } 137 138 143 public boolean getRollbackOnly() throws TransactionException { 144 boolean bolResult; 145 try { 146 bolResult = ctx.getRollbackOnly(); 147 } catch (IllegalStateException e) { 148 throw new TransactionException("There was an exception in the getRollbackOnly", e); 149 } 150 return bolResult; 151 } 152 153 162 public void insertTablesUsingAuxBeanReq(final String db1, final String db2) throws Exception { 163 insertTable(db1); 165 ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper.getBeanRemoteInstance(SLSBTransactionMisc00.class, 167 ItfTransactionMisc00.class); 168 slsbTransactionMisc00.insertTableWithRequired(db2); 170 makeSQLError(db2); 172 173 } 174 175 184 public void insertTablesUsingAuxBeanNotSup(final String db1, final String db2) throws Exception { 185 insertTable(db1); 187 ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper.getBeanRemoteInstance(SLSBTransactionMisc00.class, 189 ItfTransactionMisc00.class); 190 slsbTransactionMisc00.insertTableWithNotSupported(db2); 192 makeSQLError(db2); 194 195 } 196 197 203 public void getUserTransactionWithLookup() throws Exception { 204 try { 205 UserTransaction utx = TransactionHelper.getUserTransaction(); 206 utx.begin(); 207 utx.commit(); 208 } catch (Exception e) { 209 throw new TransactionException("The bean cannot get the user transaction with the JNDI", e); 210 } 211 } 212 213 219 public void getUserTransactionWithEJBContext() throws Exception { 220 try { 221 UserTransaction utx = ctx.getUserTransaction(); 222 utx.begin(); 223 utx.commit(); 224 } catch (Exception e) { 225 throw new TransactionException("The bean cannot get the user transaction with the EJBContext", e); 226 } 227 } 228 229 } 230 | Popular Tags |