Posted by: tonyteaching | July 28, 2009

SMS Program: Error karena Database Idle

Berikut pengalaman error aplikasi SMS Server saya, yang error (tadinya jalan lancar) gara-gara database-nya (MySQL) idle cukup lama:

28/07/2009 2:00:51 PM org.smslib.helper.Logger logInfo
INFO: InboundProcess.java: readMessages()
28/07/2009 2:01:02 PM org.smslib.helper.Logger logInfo
INFO: Mencoba baca sms
Ditemukan 1 sms
SMS yg ditemukan:
—————–
Originator : 61431522213
Text : Obm d
———————-
Kata pertama : Obm
28/07/2009 2:01:02 PM org.smslib.helper.Logger logInfo
Kata kedua : d
INFO: Mencoba parsing sms
28/07/2009 2:01:02 PM org.smslib.helper.Logger logInfo
INFO: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.

socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2670)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2601)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1565)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1675)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2295)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2228)

at com.mysql.jdbc.Statement.executeQuery(Statement.java:1159)
at InboundProcess.ComposeReplyPerRegion(InboundProcess.java:407)
at InboundProcess.parseMessage(InboundProcess.java:181)
at InboundProcess.readMessages(InboundProcess.java:119)
at InboundProcess.run(InboundProcess.java:47)

** END NESTED EXCEPTION **

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2670)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2601)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1565)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1675)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2295)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2228)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1159)
at InboundProcess.ComposeReplyPerRegion(InboundProcess.java:407)
at InboundProcess.parseMessage(InboundProcess.java:181)
at InboundProcess.readMessages(InboundProcess.java:119)
at InboundProcess.run(InboundProcess.java:47)

** END NESTED EXCEPTION **

at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2688)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2601)
Reply : null
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1565)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1675)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2295)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2228)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1159)
at InboundProcess.ComposeReplyPerRegion(InboundProcess.java:407)
at InboundProcess.parseMessage(InboundProcess.java:181)
at InboundProcess.readMessages(InboundProcess.java:119)
at InboundProcess.run(InboundProcess.java:47)
Exception in thread “Thread-7” java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException: No udData to decode
at org.ajwcc.pduUtils.gsm3040.PduGenerator.generatePduString(PduGenerator.java:520)
at org.ajwcc.pduUtils.gsm3040.PduGenerator.generatePduList(PduGenerator.java:474)
at org.smslib.OutboundMessage.getPdus(OutboundMessage.java:477)
at org.smslib.modem.ModemGateway.sendMessagePDU(ModemGateway.java:405)
at org.smslib.modem.ModemGateway.sendMessage(ModemGateway.java:205)
at org.smslib.Service.sendMessage(Service.java:567)
at InboundProcess.sendReply(InboundProcess.java:266)
at InboundProcess.parseMessage(InboundProcess.java:188)
at InboundProcess.readMessages(InboundProcess.java:119)
at InboundProcess.run(InboundProcess.java:47)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException: No udData to decode
at org.ajwcc.pduUtils.gsm3040.PduGenerator.writeUDData(PduGenerator.java:172)
at org.ajwcc.pduUtils.gsm3040.PduGenerator.generateSmsSubmitPduString(PduGenerator.java:550)
at org.ajwcc.pduUtils.gsm3040.PduGenerator.generatePduString(PduGenerator.java:502)
… 9 more
Caused by: java.lang.NullPointerException: No udData to decode
at org.ajwcc.pduUtils.gsm3040.Pdu.getDecodedText(Pdu.java:497)
at org.ajwcc.pduUtils.gsm3040.PduGenerator.writeUDData7bit(PduGenerator.java:282)
at org.ajwcc.pduUtils.gsm3040.PduGenerator.writeUDData(PduGenerator.java:158)
… 11 more

—————————–
SOLUSI:
membuat 1 class tambahan untuk me-refresh koneksi ke database setiap 1 jam sekali. Misal dikasih nama “MysqlConnRefresher.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

public class MysqlConnRefresher extends Thread
{
private Connection conn;
public void setConnection(Connection c)
{
conn = c;
}

@Override
public void run()
{
String sql;
ResultSet rs;
Statement s;
while(true)
{
try {
sql  = “select * from events”;
s = conn.createStatement();
rs = s.executeQuery(sql);
System.out.println(“Reusing MySQL Connection to avoid database connection idle”);
Thread.sleep(1000 * 60 * 60);   // setiap 1 jam (1000ms x 60 x 60)

//Thread.sleep(1000);
} catch (SQLException ex) {
Logger.getLogger(MysqlConnRefresher.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(MysqlConnRefresher.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
——————————–
Kemudian save file ini di folder file source java utama kita
Lalu tambahkan baris berikut ke dalam metod initialisasi koneksi database yakni  private void initDatabase() di file utama aplikasi SMS Server kita (dalam aplikasi saya “NTsms.java”)
MysqlConnRefresher mcr = new MysqlConnRefresher();
mcr.setConnection(conn);
mcr.start();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: