请教:jsp使用oracle数据库的问题

灵智上人 2004-09-03 09:06:23
我用的是resin做web服务器,在resin中对数据库的连接是这样设置的
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<init-param driver-name="oracle.jdbc.driver.OracleDriver"/>
<init-param url="jdbc:oracle:thin:@localhost:1521:orcl"/>
<init-param user="crmdbo"/>
<init-param password="crmdbo"/>
<init-param max-connections="1000"/>
<init-param max-idle-time="30"/>
</resource-ref>

执行对数据库操作的bean是这样写的

import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DriverManager;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.ArrayList;

public class ConnDb
{
Connection conn=null;
Statement stmt=null;
DataSource pool;

/*初始化*/
public void init()
{
try
{
Context env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/test");
} catch (NamingException e) {
}
}

/*创建连接*/
public Connection getConnection()
{
init();
Connection conn;
try
{
conn = pool.getConnection();
return conn;
}catch (Exception e){
System.out.println(e);
return null;
}
}

/*执行sql语句,返回ResultSet*/
public ResultSet executeQuery(String sqlstr)
{
try
{
conn = getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlstr);
return rs;
}catch(Exception e){
try
{
conn.close();
stmt.close();
}catch(Exception ee){
System.out.println(ee);
}
return null;
}
}

public void close()
{
try
{
stmt.close();
conn.close();
}catch(Exception ee){
System.out.println(ee);
try
{
conn.close();
stmt.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}

/*执行sql语句*/
public int executeUpdate(String sqlstr)
{
try
{
conn = getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
stmt.close();
conn.close();
return 1;
}catch(Exception e){
try{
stmt.close();
conn.close();
System.out.println(e);
}catch(Exception ee){
System.out.println(ee);
}
return 0;
}
}


private Hashtable getTheValue(ResultSet rs, ResultSetMetaData rsmd)
{
/*********从结果集中取出一条记录****************/
Hashtable hstab = new Hashtable();
try
{
if(rs != null){
int maxrows = rsmd.getColumnCount();
for (int i = 1; i <= maxrows; i++){
String key = rsmd.getColumnName(i);
String value = rs.getString(i);
if (value == null){
value = "";
}
hstab.put(key, value);
}
}
rs.close();
}catch(Exception e){
System.out.println(e.getMessage());
try
{
rs.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
return hstab;
}
return hstab;
}

public ArrayList getList(String sqlString)
{
/*********查询结果集,返回ArrayList***********/
ArrayList pkv = new ArrayList();
try
{
conn = getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlString);
ResultSetMetaData rsmd = rs.getMetaData();
int num = rsmd.getColumnCount();
while (rs.next())
{
Hashtable table = new Hashtable();
for (int i = 1;i <= num; i++)
{
String key = rsmd.getColumnName(i);
String value = rs.getString(i);
if (value==null)
value = "";
table.put(key,value);
}
pkv.add(table);
}
rs.close();
stmt.close();
conn.close();
}catch (SQLException e){
System.out.println(e);
try{
stmt.close();
conn.close();}
catch(Exception ee)
{
System.out.println(ee);
}
}
return pkv;
}

public Hashtable getValue(String sql)
{
/***************获取一条记录*******************/
Hashtable hstab = new Hashtable();

Connection con = getConnection();
Statement stmt = null;

try{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
if (rs.next()){
hstab = this.getTheValue(rs, rsmd);
}
rs.close();
stmt.close();
con.close();
return hstab;
}catch(Exception e){
System.out.println(e.getMessage());
try{
stmt.close();
con.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return hstab;
}
}

请问为什莫对数据库操作时,数据库中的线程不断增加
当操作到一定程度是,resin服务就会报超出数据库进程的错误,只有在关闭resin后数据库中的线程才会被终止
有什莫解决办法
...全文
169 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
NewTypeQ 2004-09-03
  • 打赏
  • 举报
回复
确保每次访问完数据库都finally close connection
应该是有某个连接没有及时释放 好好检查一下程序
灵智上人 2004-09-03
  • 打赏
  • 举报
回复
这个bean在sqlserver2000下没有问题
gumplei 2004-09-03
  • 打赏
  • 举报
回复
看不懂,帮顶
灵智上人 2004-09-03
  • 打赏
  • 举报
回复
请问killwin怎样在主程序中调用bean.close()
谢谢
killwin 2004-09-03
  • 打赏
  • 举报
回复
有可能是在你的程序中没有显式的调用bean.close()
你可以再检查一下你的主程序,bean好象没什么问题
灵智上人 2004-09-03
  • 打赏
  • 举报
回复
是不是写成这样啊,试了一下,还是不行啊
public int executeUpdate(String sqlstr) throws SQLException
{
/***************************************
*执行数据更新,如Update,Insert into,Delete
*执行成功返回1否则返回0
***************************************/

try{
conn = getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
return 1;
}
finally
{
stmt.close();
conn.close();
}
}
yingshis 2004-09-03
  • 打赏
  • 举报
回复
up
yunxiang 2004-09-03
  • 打赏
  • 举报
回复
up
ecaol 2004-09-03
  • 打赏
  • 举报
回复
up
dugang106 2004-09-03
  • 打赏
  • 举报
回复
try,catch模块中的所有关闭操作都放到finally
试试吧!
guestroad 2004-09-03
  • 打赏
  • 举报
回复
up
灵智上人 2004-09-03
  • 打赏
  • 举报
回复
我用的是jdk1.3+resin2.14+oracle8.05,会不会和jdbc驱动有关

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧