Improved DBBean logging, needs some more work
This commit is contained in:
parent
3f12f5cc45
commit
9108a4de20
4 changed files with 40 additions and 41 deletions
BIN
Zutil.jar
BIN
Zutil.jar
Binary file not shown.
|
|
@ -183,7 +183,7 @@ public abstract class DBBean {
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql = query.toString();
|
String sql = query.toString();
|
||||||
logger.finest("Save query("+c.getName()+" id:"+this.getId()+"): "+ sql);
|
logger.finest("Save Bean("+c.getName()+", id: "+this.getId()+") query: "+ sql);
|
||||||
PreparedStatement stmt = db.getPreparedStatement( sql );
|
PreparedStatement stmt = db.getPreparedStatement( sql );
|
||||||
// Put in the variables in the SQL
|
// Put in the variables in the SQL
|
||||||
int index = 1;
|
int index = 1;
|
||||||
|
|
@ -191,11 +191,11 @@ public abstract class DBBean {
|
||||||
|
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
if( DBBean.class.isAssignableFrom( field.getType() )){
|
if( DBBean.class.isAssignableFrom( field.getType() )){
|
||||||
DBBean subobj = (DBBean)getFieldValue(field);
|
DBBean subObj = (DBBean)getFieldValue(field);
|
||||||
if(subobj != null){
|
if(subObj != null){
|
||||||
if( recursive || subobj.getId() == null )
|
if( recursive || subObj.getId() == null )
|
||||||
subobj.save(db);
|
subObj.save(db);
|
||||||
stmt.setObject(index, subobj.getId() );
|
stmt.setObject(index, subObj.getId() );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
stmt.setObject(index, null);
|
stmt.setObject(index, null);
|
||||||
|
|
@ -204,7 +204,7 @@ public abstract class DBBean {
|
||||||
// A list of DBBeans
|
// A list of DBBeans
|
||||||
else if( List.class.isAssignableFrom( field.getType() ) &&
|
else if( List.class.isAssignableFrom( field.getType() ) &&
|
||||||
field.getAnnotation( DBLinkTable.class ) != null){
|
field.getAnnotation( DBLinkTable.class ) != null){
|
||||||
// Du stuff later
|
// Do stuff later
|
||||||
}
|
}
|
||||||
// Normal field
|
// Normal field
|
||||||
else{
|
else{
|
||||||
|
|
@ -228,34 +228,34 @@ public abstract class DBBean {
|
||||||
List<DBBean> list = (List<DBBean>)getFieldValue(field);
|
List<DBBean> list = (List<DBBean>)getFieldValue(field);
|
||||||
if( list != null ){
|
if( list != null ){
|
||||||
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
||||||
String subtable = linkTable.table();
|
String subTable = linkTable.table();
|
||||||
String idcol = (linkTable.idColumn().isEmpty() ? config.tableName : linkTable.idColumn() );
|
String idCol = (linkTable.idColumn().isEmpty() ? config.tableName : linkTable.idColumn() );
|
||||||
String sub_idcol = "id";
|
String subIdCol = "id";
|
||||||
|
|
||||||
DBBeanConfig subConfig = null;
|
DBBeanConfig subConfig = null;
|
||||||
for(DBBean subobj : list){
|
for(DBBean subObj : list){
|
||||||
// Save the sub bean
|
// Save the sub bean
|
||||||
if( recursive || subobj.getId() == null )
|
if( recursive || subObj.getId() == null )
|
||||||
subobj.save(db);
|
subObj.save(db);
|
||||||
if( subobj.getId() == null ){
|
if( subObj.getId() == null ){
|
||||||
logger.severe("Unable to save field "+c.getSimpleName()+"."+field.getName()+" with "+subobj.getClass().getSimpleName());
|
logger.severe("Unable to save field "+c.getSimpleName()+"."+field.getName()+" with "+subObj.getClass().getSimpleName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Get the Sub object configuration
|
// Get the Sub object configuration
|
||||||
if(subConfig == null){
|
if(subConfig == null){
|
||||||
subConfig = DBBeanConfig.getBeanConfig( subobj.getClass() );
|
subConfig = DBBeanConfig.getBeanConfig( subObj.getClass() );
|
||||||
sub_idcol = subConfig.idColumn;
|
subIdCol = subConfig.idColumn;
|
||||||
}
|
}
|
||||||
// Save links in link table
|
// Save links in link table
|
||||||
String subsql = "";
|
sql = "";
|
||||||
if( subtable.equals(subConfig.tableName) )
|
if( subTable.equals(subConfig.tableName) )
|
||||||
subsql = "UPDATE "+subtable+" SET "+idcol+"=? WHERE "+sub_idcol+"=?";
|
sql = "UPDATE "+subTable+" SET "+idCol+"=? WHERE "+subIdCol+"=?";
|
||||||
else
|
else
|
||||||
subsql = "REPLACE INTO "+subtable+" SET "+idcol+"=?, "+sub_idcol+"=?";
|
sql = "REPLACE INTO "+subTable+" SET "+idCol+"=?, "+subIdCol+"=?";
|
||||||
logger.finest("List Save query("+c.getName()+" id:"+subobj.getId()+"): "+subsql);
|
logger.finest("Save Bean("+c.getName()+", id: "+subObj.getId()+") query: "+sql);
|
||||||
PreparedStatement subStmt = db.getPreparedStatement( subsql );
|
PreparedStatement subStmt = db.getPreparedStatement( sql );
|
||||||
subStmt.setLong(1, this.getId() );
|
subStmt.setLong(1, this.getId() );
|
||||||
subStmt.setLong(2, subobj.getId() );
|
subStmt.setLong(2, subObj.getId() );
|
||||||
DBConnection.exec(subStmt);
|
DBConnection.exec(subStmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -279,10 +279,10 @@ public abstract class DBBean {
|
||||||
Class<? extends DBBean> c = this.getClass();
|
Class<? extends DBBean> c = this.getClass();
|
||||||
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
||||||
if( this.getId() == null )
|
if( this.getId() == null )
|
||||||
throw new NoSuchElementException("ID field is null( Has the bean been saved?)!");
|
throw new NoSuchElementException("ID field is null? (Has the bean been saved?)");
|
||||||
|
|
||||||
String sql = "DELETE FROM "+config.tableName+" WHERE "+config.idColumn+"=?";
|
String sql = "DELETE FROM "+config.tableName+" WHERE "+config.idColumn+"=?";
|
||||||
logger.fine("Delete query("+c.getName()+" id:"+this.getId()+"): "+sql);
|
logger.finest("Delete Bean("+c.getName()+", id: "+this.getId()+") query: "+sql);
|
||||||
PreparedStatement stmt = db.getPreparedStatement( sql );
|
PreparedStatement stmt = db.getPreparedStatement( sql );
|
||||||
// Put in the variables in the SQL
|
// Put in the variables in the SQL
|
||||||
stmt.setObject(1, this.getId() );
|
stmt.setObject(1, this.getId() );
|
||||||
|
|
@ -304,7 +304,7 @@ public abstract class DBBean {
|
||||||
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
||||||
// Generate query
|
// Generate query
|
||||||
String sql = "SELECT * FROM "+config.tableName;
|
String sql = "SELECT * FROM "+config.tableName;
|
||||||
logger.fine("Load All query("+c.getName()+"): "+sql);
|
logger.finest("Load all Beans("+c.getName()+") query: "+sql);
|
||||||
PreparedStatement stmt = db.getPreparedStatement( sql );
|
PreparedStatement stmt = db.getPreparedStatement( sql );
|
||||||
// Run query
|
// Run query
|
||||||
List<T> list = DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(c, db) );
|
List<T> list = DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(c, db) );
|
||||||
|
|
@ -324,7 +324,7 @@ public abstract class DBBean {
|
||||||
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
DBBeanConfig config = DBBeanConfig.getBeanConfig( c );
|
||||||
// Generate query
|
// Generate query
|
||||||
String sql = "SELECT * FROM "+config.tableName+" WHERE "+config.idColumn+"=? LIMIT 1";
|
String sql = "SELECT * FROM "+config.tableName+" WHERE "+config.idColumn+"=? LIMIT 1";
|
||||||
logger.fine("Load query("+c.getName()+" id:"+id+"): "+sql);
|
logger.finest("Load Bean("+c.getName()+", id: "+id+") query: "+sql);
|
||||||
PreparedStatement stmt = db.getPreparedStatement( sql );
|
PreparedStatement stmt = db.getPreparedStatement( sql );
|
||||||
stmt.setObject(1, id );
|
stmt.setObject(1, id );
|
||||||
// Run query
|
// Run query
|
||||||
|
|
@ -356,7 +356,8 @@ public abstract class DBBean {
|
||||||
}
|
}
|
||||||
query.delete( query.length()-2, query.length());
|
query.delete( query.length()-2, query.length());
|
||||||
query.append(")");
|
query.append(")");
|
||||||
logger.fine("Create query("+c.getName()+"): "+sql.toString());
|
|
||||||
|
logger.finest("Create Bean("+c.getName()+") query: "+sql.toString());
|
||||||
PreparedStatement stmt = sql.getPreparedStatement( sql.toString() );
|
PreparedStatement stmt = sql.getPreparedStatement( sql.toString() );
|
||||||
|
|
||||||
// Execute the SQL
|
// Execute the SQL
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class DBBeanConfig{
|
||||||
* Caches the fields
|
* Caches the fields
|
||||||
*/
|
*/
|
||||||
private static void initBeanConfig(Class<? extends DBBean> c){
|
private static void initBeanConfig(Class<? extends DBBean> c){
|
||||||
logger.fine("Initiating new DBBeanConfig( "+c.getName()+" )");
|
logger.fine("Initiating new BeanConfig( "+c.getName()+" )");
|
||||||
DBBeanConfig config = new DBBeanConfig();
|
DBBeanConfig config = new DBBeanConfig();
|
||||||
// Find the table name
|
// Find the table name
|
||||||
DBBean.DBTable tableAnn = c.getAnnotation(DBBean.DBTable.class);
|
DBBean.DBTable tableAnn = c.getAnnotation(DBBean.DBTable.class);
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,6 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
*/
|
*/
|
||||||
private static class DBBeanGarbageCollector extends TimerTask {
|
private static class DBBeanGarbageCollector extends TimerTask {
|
||||||
public void run(){
|
public void run(){
|
||||||
logger.fine("DBBean GarbageCollector has started.");
|
|
||||||
if( cache == null ){
|
if( cache == null ){
|
||||||
logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer.");
|
logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer.");
|
||||||
this.cancel();
|
this.cancel();
|
||||||
|
|
@ -165,13 +164,12 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
if( beanCache.timestamp + CACHE_TTL < time ){
|
if( beanCache.timestamp + CACHE_TTL < time ){
|
||||||
class_cache.remove(objKey);
|
class_cache.remove(objKey);
|
||||||
removed++;
|
removed++;
|
||||||
logger.finer("Removing old DBBean(class: "+beanCache.bean.getClass().getName()
|
logger.finer("Removing old Bean("+beanCache.bean.getClass().getName()+")" +
|
||||||
+", id: "+beanCache.bean.getId()+") from cache.");
|
" from cache with id: "+beanCache.bean.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( removed > 0 )
|
|
||||||
logger.info("DBBean GarbageCollector has cleared "+removed+" beans from cache.");
|
logger.info("DBBean GarbageCollector has cleared "+removed+" beans from cache.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +212,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
DBBean obj = getCachedDBBean(bean_class, id, result);
|
DBBean obj = getCachedDBBean(bean_class, id, result);
|
||||||
if( obj != null )
|
if( obj != null )
|
||||||
return obj;
|
return obj;
|
||||||
logger.fine("Creating new DBBean("+bean_class.getName()+") with id: "+id);
|
logger.fine("Creating new Bean("+bean_class.getName()+") with id: "+id);
|
||||||
obj = bean_class.newInstance();
|
obj = bean_class.newInstance();
|
||||||
cacheDBBean(obj, id);
|
cacheDBBean(obj, id);
|
||||||
|
|
||||||
|
|
@ -240,7 +238,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected void updateBean(ResultSet result, DBBean obj) throws SQLException{
|
protected void updateBean(ResultSet result, DBBean obj) throws SQLException{
|
||||||
try {
|
try {
|
||||||
logger.fine("Updating DBBean("+bean_class.getName()+") with id: "+obj.id);
|
logger.fine("Updating Bean("+bean_class.getName()+") with id: "+obj.id);
|
||||||
obj.processing_update = true;
|
obj.processing_update = true;
|
||||||
// Get the rest
|
// Get the rest
|
||||||
for( Field field : bean_config.fields ){
|
for( Field field : bean_config.fields ){
|
||||||
|
|
@ -299,7 +297,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
try{
|
try{
|
||||||
return getCachedDBBean( c, id, null );
|
return getCachedDBBean( c, id, null );
|
||||||
}catch(SQLException e){
|
}catch(SQLException e){
|
||||||
throw new RuntimeException("This exception sould not be thrown, Somting is realy wrong!", e);
|
throw new RuntimeException("This exception should not be thrown, Something ent ready wrong!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -323,7 +321,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
return null;
|
return null;
|
||||||
// Only update object if there is no update running now
|
// Only update object if there is no update running now
|
||||||
if( !item.bean.processing_update ){
|
if( !item.bean.processing_update ){
|
||||||
logger.finer("Cache to old: "+c.getName()+" ID: "+id);
|
logger.finer("Bean("+c.getName()+") cache to old for id: "+id);
|
||||||
updateBean( result, item.bean );
|
updateBean( result, item.bean );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -332,7 +330,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
// The cache is null
|
// The cache is null
|
||||||
cache.get(c).remove(id);
|
cache.get(c).remove(id);
|
||||||
}
|
}
|
||||||
logger.finer("Cache miss: "+c.getName()+" ID: "+id);
|
logger.finer("Bean("+c.getName()+") cache miss for id: "+id);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue