2014-07-13 78 views
1

當我關閉查詢後很快關閉游泳池我得到這個異常:插槽錯誤和查詢

Uncaught Error: Bad state: Cannot write to socket, it is closed 
Stack Trace: 
#0  BufferedSocket.writeBufferPart (package:sqljocky/src/buffered_socket.dart:114:7) 
#1  BufferedSocket.writeBuffer (package:sqljocky/src/buffered_socket.dart:108:27) 
#2  _Connection._sendBufferPart (package:sqljocky/src/connection.dart:261:31) 
#3  _Connection._sendBuffer (package:sqljocky/src/connection.dart:249:29) 
#4  _Connection.processHandler (package:sqljocky/src/connection.dart:289:16) 
#5  ConnectionPool._closeQuery.<anonymous closure> (package:sqljocky/src/connection_pool.dart:220:29) 
#6  _rootRunUnary (dart:async/zone.dart:730) 
#7  _RootZone.runUnary (dart:async/zone.dart:864) 
#8  _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488) 
#9  _Future._propagateToListeners (dart:async/future_impl.dart:571) 
#10  _Future._completeWithValue (dart:async/future_impl.dart:331) 
#11  _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:393) 
#12  _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23) 
#13  _asyncRunCallback (dart:async/schedule_microtask.dart:32) 
#14  _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128) 


Unhandled exception: 
Bad state: Cannot write to socket, it is closed 
#0  _rootHandleUncaughtError.<anonymous closure>.<anonymous closure> (dart:async/zone.dart:713) 
#1  _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23) 
#2  _asyncRunCallback (dart:async/schedule_microtask.dart:32) 
#3  _asyncRunCallback (dart:async/schedule_microtask.dart:36) 
#4  _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128) 

的問題似乎是一個未來的查詢接近火災內部,所以在收盤前收盤()函數返回的是實際完成:

void _closeQuery(Query q, bool retain) { 
    _log.finest("Closing query: ${q.sql}"); 
    for (var cnx in _pool) { 
    var preparedQuery = cnx.removePreparedQueryFromCache(q.sql); 
    if (preparedQuery != null) { 
     _waitUntilReady(cnx).then((_) { 
     _log.finest("Connection ready - closing query: ${q.sql}"); 
     var handler = new _CloseStatementHandler(preparedQuery.statementHandlerId); 
     cnx.autoRelease = !retain; 
     cnx.processHandler(handler, noResponse: true); 
     }); 
    } 
    } 
} 

池立即關閉發生,它關閉套接字的時候了。這意味着查詢關閉(延遲到由於未來池關閉後)失敗,無法通過套接字發送任何需要的信息。我在https://github.com/jamesots/sqljocky/issues/44上打開了sqljocky的票據,但我沒有收到回覆,如果需要一段時間才能獲得回覆,我需要一個解決方法。

此代碼已經讓我複製問題的100%的時間:

Future _putMethod(RestRequest request) { 
    return new Future.sync(() { 
    mysql.ConnectionPool pool = getConnectionPool(); 
    return pool.prepare("SELECT * FROM files").then((mysql.Query query) { 
     return query.execute().then((result) { 
     // Do something? 
     }).then((_) { 
     this._log.info("Closing"); 
     query.close(); 
     }); 
    }).then((_) { 
     pool.close(); 
    }); 
    }); 
} 

回答

0

這又是一個多答案的問題,但我不能把這個代碼在評論中可用的方法。

您應該確保您返回從每個異步調用返回的Future。 我不知道我添加註釋// added return的行是否是異步調用。

如果這改變了任何東西,你可以請嘗試並提供反饋。

Future _putMethod(RestRequest request) { 
    return new Future.sync(() { 
    mysql.ConnectionPool pool = getConnectionPool(); 
    return pool.prepare("SELECT * FROM files").then((mysql.Query query) { 
     return query.execute().then((result) { 
     // Do something? // also ensure that a Future of an async invocation is returned 
     }).then((_) { 
     this._log.info("Closing"); 
     return query.close(); // added return 
     }); 
    }).then((_) { 
     return pool.close(); // added return 
    }); 
    }); 
} 
+1

無論query.close也不pool.close返回未來。在內部他們是異步的,但他們沒有給我等待異步完成的能力。這就是我與創作者打開的票據,讓那些回報期貨。 – SanMadJack