I am using Sqlite to store Objects in my Java application and send them to clients whenever they request those objects from my Java Server . I am using below code to store objects in my sqlite database.
public void Update(String db, String sql, Object obj) {
try {
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
PreparedStatement ps = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] data = bos.toByteArray();
ps = c.prepareStatement(sql);
ps.setObject(1, data);
ps.executeUpdate();
c.commit();
settings.outPrintln(sql);
settings.outPrintln("Update Operation done successfully on DB " + db);
} catch (SQLException e) {
System.out.println(sql + " didnot executed on " + db);
settings.errPrintln(e.getClass().getName() + ": " + e.getMessage());
} catch (IOException ex) {
System.out.println(sql + " didnot executed on " + db);
Logger.getLogger(SQLiteJDBC.class.getName()).log(Level.SEVERE, null, ex);
}
}
I am successfully saving the objects upto 900 mb on my db server. But on retrival sqlite gives
Select Operation done successfully on DB server.db
Value From GetObject[[D@67ef3398#
# A fatal error has been detected by the Java Runtime Environment:
#
#
[[D@67ef3398
SIGSEGV (0xb) at pc=0x00007f0dd700de11, pid=14634, tid=139695963711232
#
# JRE version: Java(TM) SE Runtime Environment (8.0_40-b25) (build 1.8.0_40-b25)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.40-b25 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [sqlite-3.8.7-3e920a23-c002-493f-baf5-5a2f4b1ccf3e-libsqlitejdbc.so+0x1de11]
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/ds/NetBeansProjects/TestDB/hs_err_pid14634.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
And to retrive object from db i used following code
public Object getObject(long delay) {
Object value = null;
SQLiteJDBC db = new SQLiteJDBC();
try {
String dbloc = "server.db";
settings.outPrintln("Executing " + simsql + " ON " + dbloc + " after " + delay + " milliseconds");
try (ResultSet rs = db.select(simsql)) {
while (rs.next()) {
try (ByteArrayInputStream bias = new ByteArrayInputStream(rs.getBytes("VALUE")); ObjectInputStream ins = new ObjectInputStream(bias)) {
value = ins.readObject();
}
}
}
db.closeStatement();
} catch (SQLException | IOException | ClassNotFoundException ex) {
Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
}
if (value == null) {
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {
Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
}
return this.getObject((long) (this.pdelay * 1.1));
} else {
System.out.println("Value From GetObject" + value);
return value;
}
}
And to send it over socket i am using the simple code
Object obj = getObject(pdelay);
outputStream.writeObject(obj);
Is there a way i can store the object in multiple parts using sqlite and send those parts over network and reassemble on client side?
Aucun commentaire:
Enregistrer un commentaire