Commit 7a8f6a6c authored by Andre Freyssinet's avatar Andre Freyssinet

Implements loadAll method (Joram-362).

Enhances object loading (Joram-358).
parent 6d31524b
......@@ -118,6 +118,13 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
*/
public final static String JDBC_DB_UPDATE_PROP = JDBC_TRANSACTION_PREFIX + ".dbupdate";
/**
* This property allows to define the SQL statement allowing to load an entry from the table used by the module,
* by default: "SELECT content FROM <table> WHERE name=?"
* This property can be set only at first launching.
*/
public final static String JDBC_DB_LOAD_PROP = JDBC_TRANSACTION_PREFIX + ".dbload";
/**
* This property allows to define the SQL statement allowing to delete an entry in the table used by the module,
* by default: "DELETE FROM <table> WHERE name=?"
......@@ -221,6 +228,7 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
}
dbinsert = AgentServer.getProperty(JDBC_DB_INSERT_PROP);
dbupdate = AgentServer.getProperty(JDBC_DB_UPDATE_PROP);
dbload = AgentServer.getProperty(JDBC_DB_LOAD_PROP);
dbdelete = AgentServer.getProperty(JDBC_DB_DELETE_PROP);
dbclose = AgentServer.getProperty(JDBC_DB_CLOSE_PROP);
......
......@@ -30,8 +30,8 @@ import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Map;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import org.objectweb.util.monolog.api.BasicLevel;
......@@ -90,11 +90,13 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
protected String dbinsert = null;
protected String dbupdate = null;
protected String dbload = null;
protected String dbdelete = null;
protected String dbclose = null;
private PreparedStatement insertStmt = null;
private PreparedStatement updateStmt = null;
private PreparedStatement loadStmt = null;
private PreparedStatement deleteStmt = null;
@Override
......@@ -107,6 +109,11 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
return dbupdate;
}
@Override
public String getDBLoadStatement() {
return dbload;
}
@Override
public String getDBDeleteStatement() {
return dbdelete;
......@@ -136,6 +143,10 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
updateStmt = conn.prepareStatement("UPDATE " + dbtable + " SET content=? WHERE name=?");
else
updateStmt = conn.prepareStatement(dbupdate);
if (dbload == null)
loadStmt = conn.prepareStatement("SELECT content FROM " + dbtable + " WHERE name=?");
else
loadStmt = conn.prepareStatement(dbload);
if (dbdelete == null)
deleteStmt = conn.prepareStatement("DELETE FROM " + dbtable + " WHERE name=?");
else
......@@ -259,12 +270,24 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
}
/**
* Fills the list with all objects of the component whose name begins with the prefix.
* Returns true if this Transaction implementation implements an optimized loadAll method.
*
* @return false.
*/
@Override
public boolean useLoadAll() {
// TODO (AF): Changes when loadAll implementation will allows good filtering of names.
return false;
}
/**
* Fills the map with all objects of the component whose name begins with the prefix.
*
* @param prefix The prefix of searched objects.
* @param list The list of corresponding objects.
* @param map The map of corresponding objects.
*/
public void loadAll(String prefix, List list) {
@Override
public void loadAll(String prefix, Map map) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadAll(" + prefix + ")");
......@@ -298,7 +321,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
// Deserializes the object and adds it to the list.
try {
list.add(loadFromByteArray(content));
map.put(name, loadFromByteArray(content));
} catch (ClassNotFoundException | IOException exc) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: cannot retrieve content for " + name, exc);
......@@ -310,7 +333,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
s.close();
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadAll: " + list.size());
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadAll: " + map.size());
return;
} catch (SQLException sqle) {
......@@ -378,7 +401,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadByteArray(" + fname + ")");
// Searchs in the log a new value for the object.
// Searches in the log a new value for the object.
Hashtable<String, DBOperation> log = perThreadContext.get().getLog();
DBOperation op = log.get(fname);
if (op != null) {
......@@ -392,16 +415,13 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
}
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT content FROM " + dbtable + " WHERE name='" + fname + "'");
loadStmt.setString(1, fname);
ResultSet rs = loadStmt.executeQuery();
if (!rs.next()) return null;
byte[] content = rs.getBytes(1);
rs.close();
s.close();
return content;
} catch (SQLException sqle) {
......@@ -753,6 +773,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
try {
insertStmt.close();
updateStmt.close();
loadStmt.close();
deleteStmt.close();
if ((dbclose != null) && (dbclose.length() > 0)) {
......
/*
* Copyright (C) 2006 - 2019 ScalAgent Distributed Technologies
* Copyright (C) 2006 - 2020 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -40,6 +40,7 @@ public interface DBTransactionMBean extends TransactionMBean {
String getDBInsertStatement();
String getDBUpdateStatement();
String getDBLoadStatement();
String getDBDeleteStatement();
String getDBCloseStatement();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment