tomcat 6.0自带DBCP连接池,7.0已经换了新的连接池。
1、context.xml 中加入如下内容:
tomcat就会在启动的时候加载连接池。这里用到JNDI,它可以把DataSource对象放在一个tomcat容器中(JNDI容器),并为容器中的Datasource对象取一个名称。
以后程序想获得DataSource对象,之需要通过名称检索即可。
这里我起的名字是:jdbc/mysql
<Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> <Loader delegate="true" /> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="50" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/chatRoom"/> </Context>
2、然后在项目的web.xml中配置如下:
注意:只要name对应即可。
<resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
至此,tomcat的配置已经完成。
3、在程序中加载。我写了一个工具类来加载。
import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class JDBCPoolUtil { private static Context context; private static DataSource ds; //拒绝new一个实例 private JDBCPoolUtil() {}; static {//注册驱动 try { context = new InitialContext(); //前缀是:java:comp/env/ (Tomcat规定) ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql"); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } return conn; } //释放资源 public static void free(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } } } }
注意一点,只能在servlet 或 jsp中来获取连接。
因为这是在tomcat容器内的。如果在容器外调用,就会有如下异常:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at util.JDBCPoolUtil.<clinit>(JDBCPoolUtil.java:25)
at dao.impl.UserDao.addUser(UserDao.java:14)
at test.UserTest.main(UserTest.java:22)
具体的解决办法我还没找到~
如果要测试的话,建议还是写到把测试写到jsp里面吧。
mysql数据源的使用:
MysqlDataSource source = new MysqlDataSource(); source.setServerName("localhost"); source.setDatabaseName("testdb"); source.setPort(3306); source.setUser("root"); source.setPassword("root"); Connection conn = source.getConnection();