1 22 package org.jboss.test.classloader.scoping.transaction.ejb; 23 24 import java.sql.Connection ; 25 26 import javax.ejb.CreateException ; 27 import javax.ejb.EJBException ; 28 import javax.ejb.SessionBean ; 29 import javax.ejb.SessionContext ; 30 import javax.naming.InitialContext ; 31 import javax.sql.DataSource ; 32 import javax.transaction.Transaction ; 33 import javax.transaction.TransactionManager ; 34 35 import org.jboss.resource.adapter.jdbc.WrappedConnection; 36 import org.jboss.test.classloader.scoping.transaction.interfaces.TestSession; 37 38 public class TestSessionBean 39 implements SessionBean 40 { 41 SessionContext context; 42 43 public void runTest() 44 { 45 try 46 { 47 Transaction tx = getTransaction(); 48 if (tx == null) 49 throw new RuntimeException ("No transaction"); 50 String id = tx.toString(); 51 52 TestSession next = (TestSession) context.getEJBObject(); 53 int hashCode = next.invokeNext(id); 54 if (hashCode != getConnectionHashCode()) 55 throw new RuntimeException ("Not using same connection - assumes track by connection"); 56 } 57 catch (Exception e) 58 { 59 throw new EJBException (e); 60 } 61 } 62 63 public int invokeNext(String id) 64 { 65 try 66 { 67 Transaction tx = getTransaction(); 68 if (tx == null) 69 throw new RuntimeException ("No transaction"); 70 if (tx.toString().equals(id) == false) 71 throw new RuntimeException ("Expected " + id + " got " + tx.toString()); 72 73 return getConnectionHashCode(); 74 } 75 catch (Exception e) 76 { 77 throw new EJBException (e); 78 } 79 } 80 81 public void ejbCreate() 82 throws CreateException 83 { 84 } 85 86 public void setSessionContext( SessionContext context ) 87 { 88 this.context = context; 89 } 90 91 public void ejbActivate() 92 { 93 } 94 95 public void ejbPassivate() 96 { 97 } 98 99 public void ejbRemove() 100 { 101 } 102 103 private Transaction getTransaction() 104 throws Exception 105 { 106 InitialContext ctx = new InitialContext (); 107 TransactionManager tm = (TransactionManager ) ctx.lookup("java:/TransactionManager"); return tm.getTransaction(); 109 } 110 111 private int getConnectionHashCode() 112 throws Exception 113 { 114 InitialContext ctx = new InitialContext (); 115 DataSource ds = (DataSource ) ctx.lookup("java:/DefaultDS"); Connection c = ds.getConnection(); 117 try 118 { 119 WrappedConnection wc = (WrappedConnection) c; 120 return System.identityHashCode(wc.getUnderlyingConnection()); 121 } 122 finally 123 { 124 c.close(); 125 } 126 } 127 } 128 | Popular Tags |