我有一个已经运行了一段时间的java套接字。今天,我注意到大量的套接字连接,但几个小时后,我得到了太多打开的文件。我已经设置了超时,但是不知为什么它没有超时,我不知道为什么。下面是我的代码片段。在我的代码中,我注意到在日志中建立了连接,并停止在这一行-
System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection(); 可能它不会从池中获得连接,但是没有错误或任何套接字连接超时。有什么解决办法吗?
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
BufferedWriter writer1 = null;
Connection dbconn1 = null;
public void run() { // etc
writer1 = null;
String message="";
BufferedReader reader1 = null;
try {
writer1 = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
reader1 = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
receivedSocketConn1.setSoTimeout(60000);
int nextChar=0;
while ((nextChar=reader1.read()) != -1) {
message += (char) nextChar;
if (nextChar == '*'){
try{
System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
System.out.println("\n\n Checking db connection status "+dbconn1.isClosed());
if ((dbconn1 == null) || dbconn1.isClosed()) {
System.out.println("\n\n db connection status is closed");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
//other codes follow here.
}
}
catch (SQLException ex){
System.out.println("Error SQL Exception : "+ex.toString());
ex.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
catch (Exception e){
System.out.println("\nSQL Error here :");
e.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
finally{
try {
if ( dbconn1 != null ) {
dbconn1.close();
System.out.println("\n\n dbConn1 is being closed");
}
}
catch(SQLException ex){
System.out.println("SQLException has been caught for dbConn1 close");
ex.printStackTrace();
}
}
}
}
}
catch (SocketTimeoutException ex){
System.out.println("SocketTimeoutException has been caught ");
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println("IOException has been caught ");
ex.printStackTrace();
}
catch (Exception ex) {
System.out.println("Exception has been caught");
ex.printStackTrace(System.out);
}
finally{
try {
if (writer1 != null ) {
writer1.close();
}
}
catch(IOException ex){
System.out.println("IOException has been caught for finally");
ex.printStackTrace(System.out);
}
}
}
public static void main(String[] args) {
try {
// setup the connection pool
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/****"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername("***");
config.setPassword("***");
config.setMinConnectionsPerPartition(5);
config.setMaxConnectionsPerPartition(40);
config.setPartitionCount(1);
connectionPool = new BoneCP(config); // setup the connection pool
}
catch (SQLException e) {
e.printStackTrace(System.out);
}
try
{
final ServerSocket serverSocketConn = new ServerSocket(8888);
while (true)
{
try
{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e)
{
System.out.println("Socket Accepting has been caught);
e.printStackTrace(System.out);
}
}
}
catch (Exception e)
{
System.out.println("Socket Conn has been caught );
e.printStackTrace(System.out);
}
} 发布于 2015-10-28 19:26:15
它与套接字超时无关。
你在泄露数据库连接。你从泳池里得到了连接却再也回不去了。
任何返回null或关闭连接的连接池都有严重的错误。您不需要这个测试或下面的代码。
https://stackoverflow.com/questions/33395824
复制相似问题