在使用connection的method结束前将connection.close()放在try {} catch {} finally{}的finally{}模块中
解决方案 »
- 上传并预览图片
- 文件生成缩略图的问题
- 静态新闻 ”最新新闻“列表 更新
- BS结构的系统在客户端播放WAV声音文件
- 关于分页语句
- 又是有关dom4j的,源代码已经写好了,请大家帮我运行一下,看看为什么总抛出这样的异常,谢谢
- 为什么我用jsp把数据插入到mysql中,在mysql无法存入中文?
- 两个页面传递参数,如何实现选一个,传两个??谢谢指教!!!
- 在线等急…………
- 请教servlet的问题,保证给足分!!!
- 如何用Socket或者HttpURLConnection模拟浏览器建立一个连接,可以读取网页数据,并且类似浏览器打开时可以随意跳转网页,而不用再次登陆
- 关于网站的结构问题~~~!!(在线等待)
<Context path="/doc" docBase="doc" debug="0" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/MysqlDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/MysqlDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <parameter>
<name>username</name>
<value>sa</value>
</parameter> <parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter> <parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://192.168.16.100:1433;DatabaseName=MyData</value>
</parameter>
</ResourceParams>
</Context>web.xml 中
<web-app>
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MysqlDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>使用
import java.sql.*;
import javax.naming.*;public class DBSQL {
public static Connection getConnection() {
try {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/MysqlDB");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
Connection conn = ds.getConnection();
return conn;
conn.close();
conn = null;
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}finally{
if(conn != null){
conn.close();
}
}
}
}
conn.close();
conn = null;
finally{
if(conn != null){
conn.close();
}
}
在使用完连接以后如何将这个连接归还到连接池,
而不是上面各位所说的将连接关闭掉[ connection.close() ]楼主是不是这个意思?
在你代码中coon.close()
实际上并没有关闭和数据库的连接
而是将连接返回到了连接池中的连接管理机制,并由该机制完成对资源的释放
并将释放之后的资源---也就是空的CONNECT返回到空池中
所以页面或者SERVLET中调用
conn.close();
conn = null;
finally{
if(conn != null){
conn.close();
}
}此段代码时,资源就可以得到释放了
....;
rs.close();
rs=null;
.......
.......;
stmt.close();
stmt=null;
.........
conn.close();
conn=null;
............
这是写在一个Javabean 中的,在JSP上何处调用,JSP中javabean 的scope="page" ,jsp关闭后,连接释放吗?
...
...
...
...
}catch( ...){
...
...}finally{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
public static void getConnection()
{
String val="null",title="Test";
try{
Context initCtx = new InitialContext();
if(initCtx == null )
throw new Exception("No Context");
ds = (DataSource)initCtx.lookup(
"java:comp/env/jdbc/OracleDB");
if (ds != null){
conn = ds.getConnection();
} }
catch(Exception ex){
System.out.println(ex.getMessage());
}
} //关闭数据库连接
public void closeConnection()
{
try{
if(rs!=null){
rs.close();
rs=null;}
if(stmt!=null){
stmt.close() ;
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;}
}
catch(SQLException e)
{
System.out.println("Conn:"+e.getMessage() ) ;
}
finally{
if(conn!=null){
try{
conn.close();
conn=null;
}catch(SQLException e){
System.out.println("Conn:"+e.getMessage() ) ;
}
} }
}
//执行数据库查询
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
if(conn==null)
getConnection();
if(conn!=null)
{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
}
catch(SQLException ex)
{
System.out.println("executeQuery:"+ex.getMessage());
}
return rs;
}
try{
...
...
...
...
}catch( ...){
...
...}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(...){
....
}
}
public class myConnection extends Connection
{
void close()
{
DBPool.releaseConn(this);
}
}明白了吗?
调用conn.close()的时候实际上不一定调用是Connection.close这个方法