<Resource name="jdbc/DBPool" type="javax.sql.DataSource" password="root" driverClassName="com.mysql.jdbc.Driver" maxIdle="2" maxWait="5000" username="root" url="jdbc:mysql://127.0.0.1:3306/test" maxActive="4"/> |
<resource-ref> <description>MySQL DB Connection Pool</description> <res-ref-name>jdbc/DBPool</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> |
<ResourceLink name="jdbc/DBPool" type="javax.sql.DataSource" global="jdbc/DBPool"/> |
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class DBPool { private static DataSource pool; static { Context env = null; try { env = (Context) new InitialContext().lookup("java:comp/env"); pool = (DataSource)env.lookup("jdbc/DBPool"); if(pool==null) System.err.println("'DBPool' is an unknown DataSource"); } catch(NamingException ne) { ne.printStackTrace(); } } public static DataSource getPool() { return pool; } } |