/** */ /**
* @(#)ResourceManagerListener.java
*
* 资源初始化监听器
*
package kinglong.jmediasoft.servlets;
import javax.servlet. * ;
import javax.servlet.http. * ;
import com.mysql.jdbc.jdbc2.optional. * ;
public class ResourceManagerListener implements ServletContextListener {
private DataSource ds = null ;
public void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
String jdbcurl = application.getInitParameter( " jdbcURL " );
String user = application.getInitParameter( " user " );
String password = application.getInitParameter( " password " );
try {
ds = new MysqlConnectionPoolDataSource();
ds.setUrl(jdbcurl);
ds.setUser(user);
ds.setPassword(password);
} catch (Exception e) {
application.log( " 无法创建数据源: " + e.getMessage());
return ;
}
application.setAttribute( " dataSource " ,ds);//将数据源变量置于应用作用域中
}
public void contextDestroyed(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
application.removeAttribute( " dataSource " );
ds = null ;
}
}