1、在Context.xml中配置
<!--<WatchedResource>WEB-INF/web.xml</WatchedResource>-->
这一行要注释掉,不然访问Web.xml去了。在web.xml配置也可以,但节点的名称和tomcat池的不一样
<Resource type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" maxIdle="30" maxWait="5000" username="im" password="devsmsim" url="jdbc:oracle:thin:@210.51.26.184:1521:friend" maxActive="20" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" />
在server.xml不写配置了,虽然同样可在这里配置,但有时会出问题
public class DBUtils
{
private static Context ctx;
private static final Object LOCK = new Object();
private static DataSource lavaSource = null;
static
{
try
{
ctx = (Context)new InitialContext().lookup("java:comp/env");
}
catch (javax.naming.NamingException e)
{
System.out.println(e.getMessage());
}
}
/**
* 取得 lava service 数据源
*
* @return DataSource
*/
public static DataSource getIMSource()
{
if (lavaSource == null)
{
try
{
synchronized (LOCK)
{
lavaSource = (DataSource) ctx.lookup("jdbc/friend");//数据库的服务名
//lavaSource = (DataSource) ctx.lookup("jdbc/"+ Globe.getProperty("database/ImSource"));
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return lavaSource;
}
/**
* 数据连接关闭工具
*
* @param con
* @param stm
* @param res
*/
public static void closeConnection(Connection con, Statement stmt,
ResultSet rs)
{
try
{
if (rs != null)
{
rs.close();
}
}
catch (Exception e)
{
}
try
{
if (stmt != null)
{
stmt.close();
}
}
catch (Exception e)
{
}
try
{
if (con != null)
{
con.close();
}
}
catch (Exception e)
{
}
}
}