1 22 package org.jboss.test.jca.ejb; 23 24 import java.sql.Connection ; 25 26 import javax.naming.InitialContext ; 27 import javax.sql.DataSource ; 28 29 35 public class ThreadLocalDB 36 { 37 private static ThreadLocal tl = new ThreadLocal (); 38 39 public static Connection open() 40 throws Exception 41 { 42 Connection c = getConnection(); 43 if (c == null) 44 { 45 InitialContext ctx = new InitialContext (); 46 DataSource ds = (DataSource ) ctx.lookup("java:/DefaultDS"); 47 ctx.close(); 48 c = ds.getConnection(); 49 tl.set(c); 50 } 51 return c; 52 } 53 54 public static void close() 55 { 56 Connection c = getConnection(); 57 tl.set(null); 58 if (c != null) 59 { 60 try 61 { 62 c.close(); 63 } 64 catch (Exception ignored) 65 { 66 } 67 } 68 } 69 70 private static Connection getConnection() 71 { 72 return (Connection ) tl.get(); 73 } 74 } 75 | Popular Tags |