大家好我有一个基本的Storm应用程序设置,它接收推文流并将它们存储在MySQL数据库中。该应用程序在第一〜23小时左右运行良好,然后开始给出以下错误:风暴23小时后崩溃
SQL Exception
SQL State: 08003
之后,它几次死亡。我使用标准的JBDC连接器从Java连接到数据库。是用于存储和建立数据库连接功能的代码如下:
private String _db="";
private Connection conn = null;
private PreparedStatement pst = null;
public ArchiveBolt(String db){
_db = db;
}
private void setupConnection() {
//Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/twitter_recording", "root", "root");
} catch (Exception e){
e.printStackTrace();
}
}
public void execute(Tuple tuple, BasicOutputCollector collector) {
Status s = (Status) tuple.getValue(0);
//setup the connection on the first run through or if the connection got closed down
try {
setupConnection();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
}
try {
pst = conn.prepareStatement("INSERT INTO " + _db + " (tweet)" +
"VALUES (?);");
pst.setString(1, s.toString());
//execute the SQL
pst.executeUpdate();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
if(ex.getSQLState().equals("08003")){
setupConnection();
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
后很明显,这是因为一个08003的错误,我决定,如果它抛出这个错误应该重试集的崩溃但是这并没有帮助。任何人都可以指出我解决这个问题的正确方向吗?
[08003 \t连接不存在](http://dev.mysql.com/doc/refman/5。0/en/connector-odbc-reference-errorcodes.html) –
连接是否在一天中的同一时间断开?如果是这样,也许连接被某个预定的事件中断(例如,DB每天重新启动)。 –
是的,我知道这一点。这就是为什么我在遇到这个错误的情况下再次进行连接设置。如上所示'catch(SQLException ex){//处理任何错误 System.out.println(“SQLException:”+ ex.getMessage()); System.out.println(“SQLState:”+ ex.getSQLState()); System.out.println(“VendorError:”+ ex.getErrorCode()); (ex.getSQLState()。equals(“08003”)){ setupConnection(); } }' –