r853 - in trunk: wikitty-api/src/main/java/org/nuiton/wikitty/services wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc
Author: bpoussin Date: 2011-04-30 14:23:38 +0200 (Sat, 30 Apr 2011) New Revision: 853 Url: http://nuiton.org/repositories/revision/wikitty/853 Log: retour en arriere, apres relecture des spec JTA, on peut bien ferme les connexions des qu'on en a plus besoin (donc avant les commit/rollback) c'est le role du fournisseur de connexion de savoir si elle est dans une transaction ou non et de rellement la fermer ou non. Donc pour chaque getConnection ou doit avoir un finally{ close } Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/services/WikittyTransaction.java trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyExtensionStorageJDBC.java trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyStorageJDBC.java Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/services/WikittyTransaction.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/services/WikittyTransaction.java 2011-04-29 18:22:17 UTC (rev 852) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/services/WikittyTransaction.java 2011-04-30 12:23:38 UTC (rev 853) @@ -51,8 +51,6 @@ /** permet d'attacher n'importe quoi a une transaction */ protected Map<Object, Object> tagValues; - /** les connections a ferme apres un commit ou rollback */ - protected LinkedList<Connection> enlistedConnections = new LinkedList<Connection>(); /** if true begin has been called for this transaction */ protected boolean started = false; @@ -87,13 +85,6 @@ tagValues.put(tag, value); } - public void addEnlistedConnection(Connection enlistedConnection) { - if (!isStarted()) { - throw new WikittyException("You can't add connection in transaction when transaction is not begin"); - } - this.enlistedConnections.add(enlistedConnection); - } - public boolean isStarted() { return started; } @@ -137,8 +128,6 @@ setStarted(false); } catch (Exception eee) { throw new WikittyException("Error on commit JTA transaction", eee); - } finally { - close(); } } @@ -155,43 +144,8 @@ setStarted(false); } catch (Exception eee) { throw new WikittyException("Error on roolback JTA transaction", eee); - } finally { - close(); } } - /** - * Close all enlisted connection, this method is automaticaly call after - * commit or rollback. - * - * A priori normalement cela devrait etre fait automatiquement par dbcp - * avec le commit/rollback mais ce n'est pas le cas - * {@link org.apache.commons.dbcp.managed.ManagedConnection#transactionComplete()} - * ou bien qu'on puisse la fermer a la main comme on fait ici, mais on tombe - * sur des NPE :( - * - * Et si on ne les closes pas on arrive tout de suite a 160 connexion - * active rien que pour les tests :( - */ - public void close() { - Connection c; - while ((c = enlistedConnections.poll()) != null) { - try { - if (!c.isClosed()) { - c.close(); - } - } catch (Exception eee) { - if (log.isDebugEnabled()) { - // Il y a des bugs dans dbcp, avec la delegation des - // connections, il arrive que la connection sous jacente soit - // null et dans ce cas si on fait des appels sur c - // pour savoir s'il faut ou non ferme la connexion - // on a un NPE. - // Donc on met seulement en debug le log au lieu de warn. - log.debug("Can't close connection", eee); - } - } - } - } } Modified: trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyExtensionStorageJDBC.java =================================================================== --- trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyExtensionStorageJDBC.java 2011-04-29 18:22:17 UTC (rev 852) +++ trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyExtensionStorageJDBC.java 2011-04-30 12:23:38 UTC (rev 853) @@ -137,7 +137,11 @@ statement.execute(jdbcQuery.getProperty(QUERY_CREATION_EXTENSION_DATA)); } catch (Exception eee) { throw new WikittyException("Can't create table for extension storage", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } + } finally { + WikittyJDBCUtil.closeQuietly(connectionTest); } } @@ -189,6 +193,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't store extension", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -228,6 +234,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't delete extension", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -246,6 +254,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't test extension existance",eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -267,6 +277,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't retrieve all extensions id", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -292,6 +304,8 @@ } catch (SQLException eee) { throw new WikittyException(String.format( "Can't retrieve all extensions required by %s", extensionName), eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -327,6 +341,8 @@ lastVersion = tmp; } catch (SQLException eee) { throw new WikittyException("Can't get last version of extensions", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } String result = lastVersion.get(extName); @@ -385,6 +401,8 @@ } catch (SQLException eee) { throw new WikittyException(String.format("Can't load extension %s", id), eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } return result; @@ -401,6 +419,8 @@ return result; } catch (Exception eee) { throw new WikittyException("Can't clear all extension", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } } Modified: trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java =================================================================== --- trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java 2011-04-29 18:22:17 UTC (rev 852) +++ trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyJDBCUtil.java 2011-04-30 12:23:38 UTC (rev 853) @@ -233,6 +233,8 @@ * This connection is not configured, returned connection is just create connection * Don't use this method, use getConnection that change auto commit to false * + * This connection must be close be the asker after used + * * @param conf configuration * @return a new Connection (db transaction) */ @@ -306,9 +308,7 @@ WikittyManagedDataSource dataSource = dataSources.get(jdbcUrl); Connection connection = dataSource.getConnection(); - // ne surtout pas oublie d'ajouter la connection pour quelle soit - // bien fermee apres un commit ou un rollback - tx.addEnlistedConnection(connection); + return connection; } catch(Exception eee) { @@ -322,26 +322,26 @@ } } - // REMOVED because, we must used WikittyTransaction with jta management -// /** -// * Closes a connection. -// * -// * @param connection the connection to close -// */ -// public static void closeQuietly(Connection connection) { -// try { -// if (connection != null) { -// // delegate close can throw NPE -// // so this is necessary to catch all Exceptions -// connection.close(); -// } -// } catch (Exception e) { -// log.error("Exception while closing connection", e); -// } -// } + /** + * Closes a connection. + * + * @param connection the connection to close + */ + public static void closeQuietly(Connection connection) { + try { + if (connection != null) { + // delegate close can throw NPE + // so this is necessary to catch all Exceptions + connection.close(); + } + } catch (Exception e) { + log.error("Exception while closing connection", e); + } + } /** * Get a new connection instance (i.e. it opens a new transaction). + * This connection must be close be the asker after used * * @return a new Connection (db transaction) * @throws SQLException if the connection fails Modified: trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyStorageJDBC.java =================================================================== --- trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyStorageJDBC.java 2011-04-29 18:22:17 UTC (rev 852) +++ trunk/wikitty-jdbc/src/main/java/org/nuiton/wikitty/jdbc/WikittyStorageJDBC.java 2011-04-30 12:23:38 UTC (rev 853) @@ -166,7 +166,11 @@ statement.execute(jdbcQuery.getProperty(QUERY_CREATION_WIKITTY_DATA)); } catch (Exception eee) { throw new WikittyException("Can't create table for wikitty storage", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } + } finally { + WikittyJDBCUtil.closeQuietly(connectionTest); } } @@ -194,8 +198,11 @@ // column but can't store binary. If binary is not used there is // no probleme log.fatal("Can add column to store binary field. You can't use binary", eee); -// throw new WikittyException("Can't create table for wikitty storage", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } + } finally { + WikittyJDBCUtil.closeQuietly(connectionTest); } } @@ -353,6 +360,8 @@ throw eee; } catch (Exception eee) { throw new WikittyException("Can't store wikitty", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -371,6 +380,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't test wikitty existance", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -394,6 +405,8 @@ } } catch (SQLException eee) { throw new WikittyException("Can't test if wikitty is already deleted", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -429,6 +442,8 @@ } catch (SQLException eee) { throw new WikittyException(String.format( "Can't restore wikitty '%s'", id), eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -451,6 +466,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't delete wikitty", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -473,6 +490,8 @@ } } catch (SQLException eee) { throw new WikittyException("Can't scan whole wikitty", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -625,6 +644,8 @@ return result; } catch (SQLException eee) { throw new WikittyException("Can't clear wikitty data", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } } @@ -647,6 +668,8 @@ } catch (SQLException eee) { result = new DataStatistic(); log.warn("Can't retrieve statisticn data", eee); + } finally { + WikittyJDBCUtil.closeQuietly(connection); } return result; }
participants (1)
-
bpoussin@users.nuiton.org