1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.transaction; 26 27 import java.rmi.RemoteException ; 28 import java.sql.SQLException ; 29 30 import javax.annotation.Resource; 31 import javax.ejb.EJBException ; 32 import javax.ejb.Remote ; 33 import javax.ejb.SessionContext ; 34 import javax.ejb.SessionSynchronization ; 35 import javax.ejb.Stateful ; 36 import javax.ejb.TransactionAttribute ; 37 import javax.ejb.TransactionAttributeType ; 38 import javax.ejb.TransactionManagement ; 39 import javax.ejb.TransactionManagementType ; 40 import javax.naming.NamingException ; 41 42 import org.objectweb.easybeans.tests.common.db.TableManager; 43 44 52 @Stateful 53 @Remote (ItfSessionSync.class) 54 @TransactionManagement (value = TransactionManagementType.CONTAINER) 55 public class SFSBSessionSync implements ItfSessionSync, SessionSynchronization { 56 57 58 61 private String strDB1; 62 63 66 private String strDB2; 67 68 71 private boolean bolRollback; 72 73 76 @Resource 77 private SessionContext sessionContext; 78 79 85 private void insertTable(final String dbName) throws SQLException , NamingException { 86 TableManager tableManager = new TableManager(dbName); 87 tableManager.insertTable(TABLE); 88 } 89 90 96 @TransactionAttribute (value = TransactionAttributeType.MANDATORY) 97 public void insertTableMandatory() throws SQLException , NamingException { 98 insertTable(strDB2); 99 100 } 101 102 108 @TransactionAttribute (value = TransactionAttributeType.NEVER) 109 public void insertTableNever() throws SQLException , NamingException { 110 insertTable(strDB2); 111 } 112 113 119 @TransactionAttribute (value = TransactionAttributeType.NOT_SUPPORTED) 120 public void insertTableNotSupported() throws SQLException , NamingException { 121 insertTable(strDB2); 122 } 123 124 130 @TransactionAttribute (value = TransactionAttributeType.REQUIRED) 131 public void insertTableRequired() throws SQLException , NamingException { 132 insertTable(strDB2); 133 } 134 135 141 @TransactionAttribute (value = TransactionAttributeType.REQUIRES_NEW) 142 public void insertTableRequiredNew() throws SQLException , NamingException { 143 insertTable(strDB2); 144 } 145 146 152 @TransactionAttribute (value = TransactionAttributeType.SUPPORTS) 153 public void insertTableSupports() throws SQLException , NamingException { 154 insertTable(strDB2); 155 } 156 157 162 @TransactionAttribute (value = TransactionAttributeType.SUPPORTS) 163 public void startup(final String dbName1, final String dbName2) { 164 strDB1 = dbName1; 165 strDB2 = dbName2; 166 bolRollback = false; 167 } 168 169 175 public void afterBegin() throws EJBException , RemoteException { 176 try { 177 insertTable(strDB1); 178 } catch (SQLException e) { 179 throw new EJBException ("SQLError " + e.getMessage()); 180 } catch (NamingException e) { 181 throw new EJBException ("LookupError " + e.getMessage()); 182 } 183 bolRollback = false; 185 186 } 187 188 196 public void afterCompletion(final boolean commited) throws EJBException , RemoteException { 197 if (commited) { 200 throw new EJBException ("The bean cannot make the commit."); 201 } 202 bolRollback = true; 204 } 205 206 211 public void beforeCompletion() throws EJBException , RemoteException { 212 sessionContext.setRollbackOnly(); 213 } 214 215 219 public boolean isRolledback() { 220 return bolRollback; 221 } 222 223 } 224 | Popular Tags |