【发布时间】:2015-10-28 15:58:11
【问题描述】:
我有一个运行了一段时间的 java 套接字。今天我注意到建立了很多套接字连接,但是几个小时后我得到了太多打开的文件。我已经设置了 timeout 但不知何故它没有超时我不知道为什么。下面是我的代码的 sn-p。在我的代码中,我在日志中注意到连接已建立并在此行停止-
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);
}
}
【问题讨论】:
-
尝试关闭BufferedReader/Writer到
-
是的,我已经尝试过我的问题是为什么没有触发超时?
-
你使用什么样的 Socket 你在哪里使用 Socket 我在你的代码中找不到它
-
以这种方式使用套接字和数据库不是一个好主意,它非常容易出错并且难以维护。使用一些框架或 Rest 模板或其他东西。
-
嗨,我已经添加了创建数据库池的主要功能,并展示了我如何接受连接并在那里处理它们。