0
項目源代碼具有用於SQL處理的Java方法。該方法確實有效,但它使用了一個有問題的解決方法:try-catch
在正常執行的方法的最後部分阻塞。什麼是實施它的正確方法?在該方法中嘗試捕獲//這是一種解決方法。它應該如何重新修復?
public void run() {
if (running) {
return;
}
running = true;
while(null == Common.server || null == Common.database || !ConnectionsPool.isInitialized()) {
// Wait until the database is set before continuing...
try {
Thread.sleep(1000);
}
catch(Exception ex) {}
}
while(running) {
final Connections cs = ConnectionsPool.getConnections();
Connection c = null;
while(!entries.isEmpty()) {
if (null == c) {
c = cs.getConnection();
}
SQLLogEntry entry = entries.remove();
if (null != entry) {
try {
write(entry, c); //find usages
}
catch (SQLException ex) {
writeLogFile("Could not write entry to SQL", ex);
}
}
}
if (null != c) {
try {
c.commit();
}
catch (SQLException ex) {
writeLogFile("Could commit to SQL", ex);
try {
c.rollback();
}
catch (SQLException ex1) {
}
// log
final StringWriter err = new StringWriter();
ex.printStackTrace(new PrintWriter(err));
EditorTransactionUtil.writeLogFile(err.toString());
// for user
final String msg = "Exception: " + EditorUtil.getErrorMessage(ex.getMessage());
try {
SwingUtilities.invokeAndWait(() -> {
JOptionPane.showMessageDialog(null, msg);
});
}
catch (Throwable ex1) {
}
}
finally {
cs.returnConnection(c);
}
c = null;
}
synchronized(entries) {
try {
entries.wait(1000);
}
catch (InterruptedException ex) {
// This is a workaround to process this loop...
}
}
}
writeLogFile("SQLMsgLogger run loop stopping...");
}
你試試catch塊你的意思是?其中有很多...也可能是StackOverflow不是要求代碼推薦/審查的最佳社區。 – n247s
其他'entries'可能被重新分配/調用'entries.notify()'或類似的,同步的。我不知道'entries'是什麼,但是應該使用一些併發能力的隊列。整個同步塊應該消失。 –
這一個:'catch(InterruptedException ex){//這是一個解決方法來處理這個循環...}' – sixtytrees