tomcat6配置datasource
需要在我们的WebRoot目录下,新建一个META-INF的目录(假如不存在),在该目录下创建一个context.xml文件,并且在context.xml文件当添加以下的配置信息:
<?xml version="1.0" encoding="UTF-8"?><Context><Resource name="jdbc/brook" auth="Container" type="javax.sql.DataSource"maxActive="50" maxIdle="30" maxWait="10000" logAbandoned="true"username="asset" password="asset" driverClassName="oracle.jdbc.driver.OracleDriver"url="jdbc:oracle:thin:@localhost:1521:ora10g" /></Context>
?其中:
name 表示指定的jndi名称
auth 表示认证方式,一般为Container
type 表示数据源床型,使用标准的javax.sql.DataSource
maxActive 表示连接池当中最大的数据库连接
maxIdle 表示最大的空闲连接数
maxWait 当池的数据库连接已经被占用的时候,最大等待时间
logAbandoned 表示被丢弃的数据库连接是否做记录,以便跟踪
username 表示数据库用户名
password 表示数据库用户的密码
driverClassName 表示JDBC DRIVER
url 表示数据库URL地址
在以往的tomcat当中还需要在web.xml指定相应的resource,在tomcat 5.5以后的版本不写也可以,但建议还是配置。
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/brook</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
?java 获取datasource
DataSource ds = null; InitialContext ctx=new InitialContext(); Context envCtx = (Context) ctx.lookup("java:comp/env"); ds=(DataSource)envCtx.lookup("jdbc/brook"); Connection conn1 = ds.getConnection(); System.out.println("====get connection sucess");?