错误信息:
close
Already closed.
getrole
getrole
[Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt
[Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt
getrole
ResultSet is closed
close
Connection is closed.
departid1
rolename is
Invalid handle
ParserUtils: warning org.xml.sax.SAXParseException: URI was not reported to parser for entity [document]
java.sql.SQLException: ResultSet is closed
at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(JdbcOdbcResultSet.java:6650)
at sun.jdbc.odbc.JdbcOdbcResultSet.next(JdbcOdbcResultSet.java:1251)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:168)
at com.attribution.GetWorkTeamName.getWorkTeamName(GetWorkTeamName.java:30)
at org.apache.jsp.safetyworkor.setsafetyeronepoint_jsp._jspService(setsafetyeronepoint_jsp.java:402)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)tomcat连接池代码:
<parameter>
<name>maxActive</name>
<value>0</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:odbc:hbsafety</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
java代码:getrole方法: public String getrole(String username){
String role_name="";
/*--------------------查找角色---------*/ String sqlstr="Select * from user_roles where user_name='"+username+"'";
this.init(Link_db.getCon());
try{
rs=state.executeQuery(sqlstr);
if(rs.next()){
role_name =rs.getString("role_name") ;} /* 角色*/
}catch(Exception e){
System.out.println("getrole");
System.out.println(e.getMessage());
}finally
{
close();
}
return role_name;
}
close方法:
public void close(){
if(con!=null){
try{ con.close();
}
catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}}
}jsp中用到的方法: String role_name=login.getrole(username);
public void init(Connection con){
try{
state=Link_db.con.createStatement();
}catch(Exception e){
System.out.println("init");
System.out.println(e.getMessage());
} public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return stmt;
}
public void init(Connection con){
try{
state=Link_db.con.createStatement();
}catch(Exception e){
System.out.println("init");
System.out.println(e.getMessage());
} public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return stmt;
}在这里我先谢谢,jinxfei了!
public Statement getStmtread(){
try{
con=getCon();
//使用局部变量,类上不在定义stmt
Statement myStmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return myStat;
}
public static synchronized Connection getCon(){
try{
Context context = new InitialContext();
//获得数据源
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/sqlserver");
//获取连接
con = ds.getConnection(); }catch(Exception e){
System.out.println("getCon");
System.out.println(e.getMessage());
}
return con;
}statement方法:
public Statement init(Connection con){
Statement state=null;
try{
state=Link_db.getCon().createStatement();
}catch(Exception e){
System.out.println("init");
System.out.println(e.getMessage());
}
return state;
}
还应该怎么修改呀?谢了,我第一次使用连接池!
自己写的不稳定,可以使用c3p0,这是目前开源用的较多的。你的代码,如果想提高速度,可以尝试如下修改:Connection conn=null;
public static synchronized Connection getCon(){
try{
if (conn==null){
Context context = new InitialContext();
//获得数据源
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/sqlserver");
//获取连接
conn = ds.getConnection(); }catch(Exception e){
System.out.println("getCon");
System.out.println(e.getMessage());
}
}
return conn;
} statement方法:
public Statement init(Connection con){
Statement state=null;
try{
state=Link_db.getCon().createStatement();
}catch(Exception e){
System.out.println("init");
System.out.println(e.getMessage());
}
return state;
}
在同一个页面中,有时间的查询,action提交到同一个网页。第一次打开网页不会出现错误,等查询的时候就会报结果集关闭,同一个网页,同样的方法,为什么查询的时候会出错呢,就是时间不一样了呀!