我已经创建了一个运行 sql 查询的 jsp 页面。每次刷新页面时,都会创建一个新的活动会话,因此在达到最大活动会话后,数据库连接丢失,并显示以下错误:
java.sql.SQLException:侦听器拒绝连接并出现以下错误:ORA-12519,TNS:找不到合适的服务处理程序
如何阻止会话的增加。
jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="dao.Dao" %>
<%@ page import="model.Chat_model" %>
<%@ page import="java.util.*" %>
<%@ page import="model.Register_model" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<%
Dao object=new Dao();
ArrayList<Chat_model> a=new ArrayList<Chat_model>();
String email1=(String)session.getAttribute("email1");
a=object.selectChat(email1, ((Register_model)(session.getAttribute("user"))).getEmail());
String name1=(String)session.getAttribute("name1");
String email2=((Register_model)(session.getAttribute("user"))).getEmail();
String name2=((Register_model)(session.getAttribute("user"))).getName();
for(Chat_model k:a)
{
if(email1.equals(k.getEmail1()))
{
out.println("<tr><td><b><u>"+name1+"</u>:</b></td><td>"+k.getChat()+"</td> </tr>");
}
else
{
out.println("<tr><td><b><u>"+name2+"</u>:</b></td><td>"+k.getChat()+"</td><tr>");
}
}
%>
</table>
<form action="../Controller" method="post">
<textarea name="chatbox" rows="4" cols="50"></textarea>
<input type="hidden" name="action" value="chatsend"></input>
<input type="submit" value="Send"></input>
</form>
</body>
</html>
数据库事务方法:
public ArrayList<Chat_model> selectChat(String s1,String s2)
{
DatabaseConnection db=new DatabaseConnection();
Connection conn=db.getConnection1();
Statement stmt = null;
String email1 = null;
String chat=null;
ArrayList<Chat_model> a=new ArrayList<Chat_model>();
try
{
stmt = conn.createStatement();
String sql = "SELECT email1,chat FROM chat_all where email1="+"'"+s1+"' AND email2="+"'"+s2+"' OR email1="+"'"+s2+"' AND email2="+"'"+s1+"' order by tm_stamp";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
Chat_model object=new Chat_model();
email1=rs.getString("email1");
chat=rs.getString("chat");
object.setEmail1(email1);
object.setChat(chat);
a.add(object);
}
}
catch(Exception e)
{
e.getStackTrace();
}
return a;
}
数据库连接类:
public class DatabaseConnection {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@siddhartha-PC:1521:xe";
String username = "system";
String password = "password";
Connection conn = null;
public Connection getConnection1(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public boolean closeConnection(Connection conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}