1 22 package org.jboss.test.jca.ejb; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 27 import javax.ejb.CreateException ; 28 import javax.ejb.EJBException ; 29 import javax.ejb.SessionBean ; 30 import javax.ejb.SessionContext ; 31 import javax.naming.InitialContext ; 32 import javax.sql.DataSource ; 33 34 40 public class UnshareableConnectionStatefulBean 41 implements SessionBean 42 { 43 44 private static final long serialVersionUID = 1L; 45 46 Connection c; 47 48 public void runTestPart1() 49 { 50 try 51 { 52 if (c.getAutoCommit()) 53 throw new EJBException ("Autocommit should be off"); 54 } 55 catch (SQLException e) 56 { 57 throw new EJBException (e.toString()); 58 } 59 } 60 61 public void runTestPart2() 62 { 63 try 64 { 65 c.commit(); 66 } 67 catch (SQLException e) 68 { 69 throw new EJBException (e.toString()); 70 } 71 } 72 73 public void ejbCreate() 74 throws CreateException 75 { 76 initConnection(); 77 } 78 79 public void ejbActivate() 80 { 81 initConnection(); 82 } 83 84 public void ejbPassivate() 85 { 86 termConnection(); 87 } 88 89 public void ejbRemove() 90 { 91 termConnection(); 92 } 93 94 public void initConnection() 95 { 96 if (c != null) 97 throw new EJBException ("Connection already inited"); 98 99 try 100 { 101 DataSource ds = (DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/DataSource"); 102 c = ds.getConnection(); 103 c.setAutoCommit(false); 104 } 105 catch (Exception e) 106 { 107 throw new EJBException (e.toString()); 108 } 109 } 110 111 public void termConnection() 112 { 113 if (c == null) 114 throw new EJBException ("Connection already terminated"); 115 116 try 117 { 118 c.close(); 119 c = null; 120 } 121 catch (Exception e) 122 { 123 throw new EJBException (e.toString()); 124 } 125 } 126 127 public void setSessionContext(SessionContext ctx) 128 { 129 } 130 131 public void unsetSessionContext() 132 { 133 } 134 } 135 136 | Popular Tags |