r2165 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
Author: tchemit Date: 2010-12-06 16:16:55 +0100 (Mon, 06 Dec 2010) New Revision: 2165 Url: http://nuiton.org/repositories/revision/topia/2165 Log: Evolution #1131: Can desactivate the flush mode auto Clean TopiaContextImpl Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2010-12-02 13:42:29 UTC (rev 2164) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2010-12-06 15:16:55 UTC (rev 2165) @@ -101,8 +101,6 @@ import static org.nuiton.i18n.I18n._; /** - * TODO-fdesbois-20100507 : Need translation of javadoc. - * <p/> * Le TopiaContextImpl est le point d'entre pour acceder aux donnees. Il est * configurer par un fichier de propriete * <p/> @@ -123,10 +121,11 @@ * Created: 23 déc. 2005 16:58:50 * * @author poussin <poussin@codelutin.com> - * @author tchemit <tchemit@codelutin.com> - * @author fdesbois <fdesbois@codelutin.com> + * @author tchemit <chemit@codelutin.com> + * @author fdesbois <desbois@codelutin.com> * @version $Id$ */ +//TODO-fdesbois-20100507 : Need translation of javadoc. public class TopiaContextImpl implements TopiaContextImplementor { /** to use log facility, just put in your code: log.info(\"...\"); */ @@ -159,6 +158,21 @@ /** Indique si le contexte a ete ferme */ protected boolean closed; + /** + * This flag permits to use (or not) the flush mode when doing queries. + * + * The normal usage is to says yes (that's why the default value is + * {@code true}), in that case whebn doing queries (says in method + * {@link #find(String, Object...)} or {@link #find(String, int, int, Object...)}) + * it will use the flush mode {@link FlushMode#AUTO}). + * + * But sometimes, when doing a lot of queries (for some imports for example), + * we do NOT want the session to be flushed each time we do a find, then you + * can set this flag to {@code false} using the method {@link #setUseFlushMode(boolean)} + * @since 2.5 + */ + protected boolean useFlushMode = true; + /** Propriete de configuration */ protected Properties config; @@ -303,7 +317,7 @@ public Map<String, TopiaService> getServices() { TopiaContextImplementor parent = getParentContext(); - Map<String, TopiaService> result = null; + Map<String, TopiaService> result; if (parent != null) { result = parent.getServices(); } else { @@ -382,7 +396,7 @@ public Set<TopiaContextImplementor> getChildContext() { // fdesbois-20100421 : Ano #546 // Copy the childContext into a new set - final Set<TopiaContextImplementor> values; + Set<TopiaContextImplementor> values; // Synchronize copy to be thread-safe during iteration synchronized (childContext) { values = new HashSet<TopiaContextImplementor>(childContext); @@ -424,6 +438,17 @@ return config; } + /** + * Change the value of flag {@link #useFlushMode}. + * + * @param useFlushMode the new value to set + * @see #useFlushMode + * @since 2.5 + */ + public void setUseFlushMode(boolean useFlushMode) { + this.useFlushMode = useFlushMode; + } + /* -------------------- HIBERNATE MANAGMENT -----------------------------*/ @Override @@ -456,10 +481,6 @@ } } - /* (non-Javadoc) - * @see org.nuiton.topia.TopiaContext#createSchema() - */ - @Override public void updateSchema() throws TopiaException { try { @@ -478,12 +499,6 @@ } } - /* - * (non-Javadoc) - * - * @see org.nuiton.topia.framework.TopiaContextImplementor#getHibernate() - */ - @Override public Session getHibernate() throws TopiaException { if (hibernate == null) { @@ -493,12 +508,6 @@ return hibernate; } - /* - * (non-Javadoc) - * - * @see org.nuiton.topia.framework.TopiaContextImplementor#getHibernateFactory() - */ - @Override public SessionFactory getHibernateFactory() throws TopiaNotFoundException { if (hibernateFactory == null) { @@ -677,7 +686,7 @@ I18n._("topia.persistence.error.null.param", "entityClass", "getDAO")); } - if (getRootContext() == this) { + if (this == getRootContext()) { throw new TopiaException( I18n._("topia.persistence.error.rootContext.access")); } @@ -725,12 +734,6 @@ return (D) getDAO(entityClass); } - /* - * (non-Javadoc) - * - * @see TopiaContext#beginTransaction() - */ - @Override public TopiaContext beginTransaction() throws TopiaException { checkClosed(I18n._("topia.persistence.error.context.is.closed")); @@ -743,6 +746,9 @@ // on ne synchronise jamais les données avec la base tant que // l'utilisateur n'a pas fait de commit du context result.hibernate.setFlushMode(FlushMode.MANUAL); + + // tchemit 2010-12-06 propagates the value of the flag + result.useFlushMode = useFlushMode; // 20060926 poussin ajouter pour voir si ca regle les problemes de // deadlock h2. Conclusion, il faut bien ouvrir une transaction @@ -759,12 +765,6 @@ return result; } - /* - * (non-Javadoc) - * - * @see TopiaContext#commitTransaction() - */ - @Override public void commitTransaction() throws TopiaException { if (getRootContext() == this) { @@ -813,7 +813,7 @@ @Override public void rollbackTransaction() throws TopiaException { - if (this.equals(getRootContext())) { + if (equals(getRootContext())) { throw new TopiaException(I18n._( "topia.persistence.error.unsupported.operation.on.root.context", "rollback")); @@ -887,9 +887,7 @@ } /** - * Pour le context root on ferme tous les fils, et la factory hibernate - * - * @see Object#finalize() + * Pour le context root on ferme tous les fils, et la factory hibernate. */ @Override protected void finalize() throws Throwable { @@ -939,11 +937,6 @@ return new TopiaQuery((Class<? extends TopiaEntity>)entityClass, alias); } - /* - * (non-Javadoc) - * @see TopiaContext#find(java.lang.String, java.lang.Object[]) - */ - @Override public List find(String hql, Object... args) throws TopiaException { checkClosed(I18n._( @@ -962,7 +955,9 @@ } } // tchemit 2010-11-30 reproduce the same behaviour than before with the dao legacy - query.setFlushMode(FlushMode.AUTO); + if (useFlushMode) { + query.setFlushMode(FlushMode.AUTO); + } List result = query.list(); result = firesSupport.fireEntitiesLoad(this, result); return result; @@ -993,7 +988,9 @@ query.setFirstResult(startIndex); query.setMaxResults(endIndex - startIndex + 1); // tchemit 2010-11-30 reproduce the same behaviour than before with the dao legacy - query.setFlushMode(FlushMode.AUTO); + if (useFlushMode) { + query.setFlushMode(FlushMode.AUTO); + } List result = query.list(); result = firesSupport.fireEntitiesLoad(this, result); return result; @@ -1241,11 +1238,13 @@ } /** - * Backup database in gzip compressed file Only work for h2 database + * Backup database in gzip compressed file. * + * <b>Note: </b> Only works for h2 database. + * * @param file file to write backup * @param compress if true then use gzip to compress file - * @see TopiaContext#backup(java.io.File,boolean) + * @see TopiaContext#backup(File,boolean) */ @Override public void backup(File file, boolean compress) throws TopiaException { @@ -1274,7 +1273,7 @@ * <p/> * Only work for h2 database * - * @see org.nuiton.topia.TopiaContext#restore(java.io.File) + * @see TopiaContext#restore(File) */ @Override public void restore(File file) throws TopiaException { @@ -1285,23 +1284,26 @@ "restore")); String sql = null; + String options = ""; try { // decompresse file in temporary file InputStream in = new BufferedInputStream(new FileInputStream(file)); + try { in.mark(2); - // read header to see if is compressed file - int b = in.read(); - // redundant cast : int magic = ((int) in.read() << 8) | b; - int magic = (in.read() << 8) | b; - in.reset(); + // read header to see if is compressed file + int b = in.read(); + // redundant cast : int magic = ((int) in.read() << 8) | b; + int magic = (in.read() << 8) | b; + in.reset(); - String options = ""; + if (magic == GZIPInputStream.GZIP_MAGIC) { + options += " COMPRESSION GZIP"; + } + } finally { - if (magic == GZIPInputStream.GZIP_MAGIC) { - options += " COMPRESSION GZIP"; + in.close(); } - in.close(); SQLQuery query = getHibernate().createSQLQuery( "RUNSCRIPT FROM '" + file.getAbsolutePath() + "'" + options); @@ -1319,7 +1321,7 @@ /** * Only h2 supported for now * - * @see org.nuiton.topia.TopiaContext#clear(boolean) + * @see TopiaContext#clear(boolean) */ @Override public void clear(boolean dropDatabase) throws TopiaException { Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2010-12-02 13:42:29 UTC (rev 2164) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2010-12-06 15:16:55 UTC (rev 2165) @@ -52,11 +52,15 @@ import java.util.Set; /** - * TODO-fdesbois-20100507 : Need more javadoc. + * Technical contract of a {@link TopiaContext}. * + * Any implementation of the {@link TopiaContext} should also implements this + * contract. + * * @author poussin <poussin@codelutin.com> * @version $Id$ */ +//TODO-fdesbois-20100507 : Need more javadoc. public interface TopiaContextImplementor extends TopiaContext { /** @@ -94,6 +98,16 @@ throws TopiaNotFoundException; /** + * Tells to the context if it has to use a flush mode before each query. + * + * By default, we use a flush mode, but in some case it costs to much doing + * this, that's why you can desactivate it setting the value to {@code false}. + * + * @param useFlushMode the new value to set + * @since 2.5 + */ + void setUseFlushMode(boolean useFlushMode); + /** * Detect if the table is created on storage for a given persistant class. * * @param clazz the researched class @@ -106,10 +120,10 @@ * Get DAO for specified class. If Specialized DAO exists then it returned * otherwize TopiaDAO<entityClass> is returned * - * @param <E> - * @param entityClass - * @return a doa - * @throws TopiaException + * @param <E> type of entity + * @param entityClass type of entity + * @return the required dao + * @throws TopiaException if any error */ <E extends TopiaEntity> TopiaDAO<E> getDAO(Class<E> entityClass) throws TopiaException; @@ -118,11 +132,11 @@ * Get DAO for specified class. If Specialized DAO exists then it returned * otherwize TopiaDAO<entityClass> is returned * - * @param <E> - * @param entityClass - * @param daoClass - * @return a doa - * @throws TopiaException + * @param <E> type of entity + * @param entityClass type of entity + * @param daoClass the concrete dao class to use + * @return the required dao + * @throws TopiaException if any error */ <E extends TopiaEntity, D extends TopiaDAO<E>> D getDAO(Class<E> entityClass,Class<D> daoClass) throws TopiaException;
participants (1)
-
tchemit@users.nuiton.org