Index: topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBC.java diff -u topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBC.java:1.11 topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBC.java:1.12 --- topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBC.java:1.11 Thu Aug 4 16:55:38 2005 +++ topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBC.java Thu Aug 4 20:59:31 2005 @@ -23,10 +23,10 @@ * Created: 20 juillet 2005 15:25:06 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ * - * Last update: $Date: 2005/08/04 16:55:38 $ - * by : $Author: thimel $ + * Last update: $Date: 2005/08/04 20:59:31 $ + * by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; @@ -43,6 +43,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -143,7 +144,7 @@ SQL_DEFAULT.put(SQL_TABLE_EXISTS, "SELECT count(*) FROM management where id='' AND longdate=0"); SQL_DEFAULT.put(SQL_CREATE_TABLE_MANAGEMENT, - "CREATE TABLE management (id text, longdate bigint, class text, isNew boolean, isDeleted boolean, schemaVersion bigint, PRIMARY KEY (id, longdate))"); + "CREATE TABLE management (id text, longdate bigin1t, class text, isNew boolean, isDeleted boolean, schemaVersion bigint, PRIMARY KEY (id, longdate))"); // sans doute faire une suppression en cascade lors que la cle(id, date), n'existe plus dans management // si supporté par toutes les bases cibles SQL_DEFAULT.put(SQL_CREATE_TABLE_DATA, @@ -158,25 +159,29 @@ "CREATE INDEX index_data_longdate ON data (longdate)"); // on tri sur la valeur abasolue puis sur la valeur reel, pour que les transactions arrive en premier dans les resultats SQL_DEFAULT.put(SQL_LOAD_HISTORY, - "SELECT * FROM management WHERE id=? AND ((longdate > 0 and longdate<=?) or longdate=?) order by abs(longdate) desc, longdate"); + "SELECT * FROM management WHERE id=? AND ((longdate > 0 and longdate<=?) or longdate in (?)) order by abs(longdate) desc, longdate"); SQL_DEFAULT.put(SQL_REMOVE_DATA, "DELETE data WHERE id=? and longdate=?"); SQL_DEFAULT.put(SQL_GET_ALL_ID, - "SELECT DISTINCT(id) FROM management WHERE (longdate=? OR (longdate>0 AND longdate<=?)) AND id NOT IN (SELECT id FROM management WHERE (longdate=? OR (longdate>0 AND longdate<=?)) AND isDeleted=true)"); + "SELECT DISTINCT(id) FROM management WHERE (longdate in (?) OR (longdate>0 AND longdate<=?)) AND id NOT IN (SELECT id FROM management WHERE (longdate in (?) OR (longdate>0 AND longdate<=?)) AND isDeleted=true)"); + SQL_DEFAULT.put(SQL_INSERT_DATA, "INSERT INTO data VALUES (?, ?, ?, ?, ?)"); SQL_DEFAULT.put(SQL_INSERT_MANAGEMENT, "INSERT INTO management VALUES (?, ?, ?, ?, ?, ?)"); + SQL_DEFAULT.put(SQL_LOAD_DATA, "SELECT * FROM data WHERE id=? AND longdate=?"); SQL_DEFAULT.put(SQL_LOAD_MANAGEMENT, "SELECT * FROM management WHERE id=? AND longdate=?"); + SQL_DEFAULT.put(SQL_UPDATE_DATA, "UPDATE data SET value=?, type=? WHERE id=? AND longdate=? AND field=?"); SQL_DEFAULT.put(SQL_UPDATE_MANAGEMENT, "UPDATE management SET isDeleted=? WHERE id=? AND longdate=?"); SQL_DEFAULT.put(SQL_UPDATE_SCHEMAVERSION, "UPDATE management SET schemaVersion=? WHERE id=? AND longdate=?"); + SQL_DEFAULT.put(SQL_ROLLBACK_MANAGEMENT, "DELETE FROM management WHERE longdate=?"); SQL_DEFAULT.put(SQL_ROLLBACK_DATA, @@ -186,6 +191,7 @@ SQL_DEFAULT.put(SQL_COMMIT_DATA, "UPDATE data SET longdate=? WHERE longdate=?"); + SQL_MYSQL.setProperty(SQL_CREATE_TABLE_MANAGEMENT, "CREATE TABLE management (id varchar(100), longdate bigint, class text, isNew bool, isDeleted bool, schemaVersion bigint, PRIMARY KEY (id, longdate));"); SQL_MYSQL.put(SQL_CREATE_TABLE_DATA, @@ -198,10 +204,10 @@ "CREATE TABLE management (id varchar(100), longdate number(21), class varchar(255), isNew number(1), isDeleted number(1), schemaVersion number(21), constraint management_pk PRIMARY KEY (id, longdate))"); SQL_ORACLE.put(SQL_CREATE_TABLE_DATA, "CREATE TABLE data (id varchar(100), longdate number(21), field varchar(100), val blob, type varchar(255), constraint data_pk PRIMARY KEY (id, longdate, field))"); - + SQL_DERBY.put(SQL_CREATE_TABLE_MANAGEMENT, "CREATE TABLE management (id VARCHAR(50), longdate bigint, class LONG VARCHAR, isNew integer, isDeleted integer, schemaVersion bigint, PRIMARY KEY (id, longdate))"); SQL_DERBY.put(SQL_CREATE_TABLE_DATA, "CREATE TABLE data (longdate bigint, field varchar(100), value blob, type LONG VARCHAR, PRIMARY KEY (id, longdate, field))"); - + } @@ -227,7 +233,7 @@ SQL = SQL_DERBY; } else { SQL = SQL_POSTGRESQL; - } + } Util.getClazz(driver); @@ -366,7 +372,7 @@ conn.close(); } catch(SQLException zzz){ - log.log(Level.INFO, "erreur précédente", eee); + log.log(Level.INFO, "erreur précédente", eee); log.log(Level.INFO, "Erreur durant le rollback du a une exception", zzz); } } @@ -439,7 +445,7 @@ sta = conn.prepareStatement(SQL.getProperty(SQL_LOAD_HISTORY)); sta.setString(1, tpo.getManagement().getId()); sta.setLong(2, -context.getTransaction().getId()); - sta.setLong(3, context.getTransaction().getId()); + sta.setObject(3, context.getTransaction().getIds(), Types.ARRAY); management = sta.executeQuery(); if(!management.next()){ // on a pas trouvé d'objet sattisfaisant la demande @@ -735,9 +741,9 @@ try{ PreparedStatement sta = conn.prepareStatement(SQL.getProperty(SQL_GET_ALL_ID)); try{ - sta.setLong(1, context.getTransaction().getId()); + sta.setObject(1, context.getTransaction().getIds(), Types.ARRAY); sta.setLong(2, -context.getTransaction().getId()); - sta.setLong(3, context.getTransaction().getId()); + sta.setObject(3, context.getTransaction().getIds(), Types.ARRAY); sta.setLong(4, -context.getTransaction().getId()); ResultSet rr = sta.executeQuery(); while(rr.next()){ @@ -784,7 +790,7 @@ try{ PreparedStatement sta = conn.prepareStatement(SQL.getProperty(SQL_COMMIT_MANAGEMENT)); try{ - sta.setLong(1, -newtt.getId()); + sta.setLong(1, newtt.getCommitId()); sta.setLong(2, context.getTransaction().getId()); sta.execute(); } finally { @@ -792,9 +798,10 @@ } // normalement plus besoin avec l'update en cascade // on le laisse au cas ou + // pour des raisons de perf, on pourra peut-etre l'enlever sta = conn.prepareStatement(SQL.getProperty(SQL_COMMIT_DATA)); try{ - sta.setLong(1, -newtt.getId()); + sta.setLong(1, newtt.getCommitId()); sta.setLong(2, context.getTransaction().getId()); sta.execute(); } finally { @@ -807,9 +814,9 @@ // peut-etre faire quelque chose comme dans beginTransaction ? context.setTransaction(newtt); } catch(SQLException eee){ - eee.printStackTrace(); + eee.printStackTrace(); throw new TopiaPersistenceException("Erreur durant le rollback transaction: " + context.getTransaction().getId(), eee); - + } } @@ -951,7 +958,7 @@ try{ sta.setString(1, id); sta.setLong(2, -context.getTransaction().getId()); - sta.setLong(3, context.getTransaction().getId()); + sta.setObject(3, context.getTransaction().getIds(), Types.ARRAY); ResultSet rr = sta.executeQuery(); if(rr.next()){ result = !rr.getBoolean("isDeleted"); Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.3 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.4 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.3 Wed Aug 3 16:13:12 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java Thu Aug 4 20:59:31 2005 @@ -23,10 +23,10 @@ * Created: 16 juillet 2005 23:42:14 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Last update: $Date: 2005/08/03 16:13:12 $ - * by : $Author: thimel $ + * Last update: $Date: 2005/08/04 20:59:31 $ + * by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; @@ -68,6 +68,14 @@ public void put(String id, TopiaEntity e){ cache.put(id, e); + } + + public void clean(){ + cache.clear(); + } + + public void putAll(TopiaPersistenceCache cache){ + cache.putAll(cache); } } // TopiaPersistenceCache Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.6 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.7 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.6 Wed Aug 3 16:17:10 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java Thu Aug 4 20:59:31 2005 @@ -23,10 +23,10 @@ * Created: Jul 16, 2005 * * @author Benjamin POUSSIN - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Last update : $Date: 2005/08/03 16:17:10 $ - * by : $Author: thimel $ + * Last update : $Date: 2005/08/04 20:59:31 $ + * by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; @@ -95,16 +95,40 @@ return cache; } + /** + * Retourne le cache du pere, s'il y un pere et qu'il n'est pas le root context + * sur lequel l'utilisateur n'a le droit de rien faire + * @return le cache du pere ou null + */ + protected TopiaPersistenceCache getParentCache() throws TopiaNotFoundException { + TopiaPersistenceCache result = null; + TopiaContext context = getContext().getParentContext(); + if(context != null && context.getParentContext() != null){ + TopiaPersistenceHelper tph = (TopiaPersistenceHelper) + context.getPersistenceHelper(); + result = tph.getCache(); + } + return result; + } + public void beginTransaction() throws TopiaException { getStorage().beginTransaction(getContext()); } public void commitTransaction() throws TopiaException { getStorage().commitTransaction(getContext()); + + // si on a un pere il faut mettre a jour son cache avec nos nouvelles + // info + TopiaPersistenceCache pc = getParentCache(); + if(pc != null){ + pc.putAll(getCache()); + } } public void rollbackTransaction() throws TopiaException { getStorage().rollbackTransaction(getContext()); + getCache().clean(); } /** Index: topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.2 topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.3 --- topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.2 Wed Jul 20 12:49:52 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java Thu Aug 4 20:59:31 2005 @@ -23,9 +23,9 @@ * Created: 16 juillet 2005 22:54:16 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2005/07/20 12:49:52 $ + * Last update: $Date: 2005/08/04 20:59:31 $ * by : $Author: bpoussin $ */ @@ -62,6 +62,47 @@ return id; } + /** + * Donne l'id a utiliser pour commiter la transaction. + * l'id est different s'il y a un parent ou qu'il n'y en a pas. + * S'il n'y a pas de parent on prend juste l'inverse de l'id. + * s'il y a un parent on prend l'id parent car les objets de la transaction + * ne doivent pas sortir de la transaction qui l'englobe + */ + public long getCommitId(){ + if(getParent() == null){ + return -getId(); + } else { + return getParent().getId(); + } + } + + /** + * Retourne tous l'id de la transaction et de ces parents + */ + public long [] getIds(){ + long [] result = new long[getAncestorCount() + 1]; + TopiaTransaction tt = this; + int i = 0; + result[i++] = tt.getId(); + while(getParent() != null){ + tt = tt.getParent(); + result[i++] = tt.getId(); + } + return result; + } + + /** + * Retourne le nombre de parent, s'il n'y en a pas, retourne 0 + */ + protected int getAncestorCount(){ + if(getParent() != null){ + return getParent().getAncestorCount() + 1; + } else { + return 0; + } + } + public TopiaTransaction getParent(){ return parent; } @@ -84,9 +125,10 @@ getParent().getChilds().remove(this); result = getParent().newSubTransaction(); } + // si on a des enfants, il faut aussi les garder :) + result.getChilds().addAll(getChilds()); return result; } - public int compareTo(TopiaTransaction o){ if(getId() < o.getId()){