我在 contex.xml 中写:
<Resource name="1_db" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="root" password="tunespray2008" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/1_db">
<Resource name="2_db" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="root" password="tunespray2008" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/2_db">
<Resource name="3_db" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="root" password="tunespray2008" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/3_db">
<Resource name="common" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="root" password="tunespray2008" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/common">
Java代码:
public static Connection getDBConnection(String url)
{
Connection con = null;
try
{
Context ctx = new InitialContext();
BasicDataSource ds = (BasicDataSource)tx.lookup("java:comp/env/"+url);
con = ds.getConnection();
}catch(Exception e) {}
return con;
}
之后我打电话:
String url ="common";
LoginDAO ldcom = DAOFactory.getLoginDAO(url);
url ="1_db";
LoginDAO ldcom = DAOFactory.getLoginDAO(url);
StatusDAO ldcom = DAOFactory.getStatusDAO(url);
之后,当我们查看JProfiler
它时,它会显示很多打开的连接,尽管我们调用con.close()
,rs.close()
或st.close()
.
请提及我们如何Datasource
以正确的方式使用 a ?