Author: sdumitriu
Date: 2007-12-13 18:02:33 +0100 (Thu, 13 Dec 2007)
New Revision: 6370
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
Log:
[cleanup] Apply codestyle
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
2007-12-13 17:01:06 UTC (rev 6369)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
2007-12-13 17:02:33 UTC (rev 6370)
@@ -1,61 +1,70 @@
package com.xpn.xwiki.store;
-import com.xpn.xwiki.XWiki;
-import com.xpn.xwiki.XWikiContext;
-import com.xpn.xwiki.XWikiException;
-import com.xpn.xwiki.monitor.api.MonitorPlugin;
-import com.xpn.xwiki.objects.classes.BaseClass;
-import com.xpn.xwiki.store.DatabaseProduct;
-import com.xpn.xwiki.util.Util;
-import com.xpn.xwiki.web.XWikiRequest;
+import java.io.File;
+import java.lang.reflect.Proxy;
+import java.net.URL;
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
-import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.hibernate.*;
-import org.hibernate.mapping.Table;
-import org.hibernate.jdbc.ConnectionManager;
-import org.hibernate.jdbc.BorrowedConnectionProxy;
+import org.hibernate.FlushMode;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.impl.SessionImpl;
+import org.hibernate.jdbc.BorrowedConnectionProxy;
+import org.hibernate.jdbc.ConnectionManager;
+import org.hibernate.mapping.Table;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
-import java.io.File;
-import java.net.URL;
-import java.sql.Connection;
-import java.sql.Statement;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Map;
-import java.util.Iterator;
-import java.lang.reflect.Proxy;
+import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.monitor.api.MonitorPlugin;
+import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.util.Util;
+import com.xpn.xwiki.web.XWikiRequest;
-public class XWikiHibernateBaseStore {
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+
+public class XWikiHibernateBaseStore
+{
private static final Log log =
LogFactory.getLog(XWikiHibernateBaseStore.class);
private Map connections = new ConcurrentHashMap();
+
private int nbConnections = 0;
+
private SessionFactory sessionFactory;
+
private Configuration configuration;
private String hibpath;
+
private URL hiburl;
/**
- * THis allows to initialize our storage engine.
- * The hibernate config file path is taken from xwiki.cfg
- * or directly in the WEB-INF directory.
+ * THis allows to initialize our storage engine. The hibernate config file
path is taken from
+ * xwiki.cfg or directly in the WEB-INF directory.
+ *
* @param xwiki
* @param context
*/
- public XWikiHibernateBaseStore(XWiki xwiki, XWikiContext context) {
+ public XWikiHibernateBaseStore(XWiki xwiki, XWikiContext context)
+ {
String path = xwiki.Param("xwiki.store.hibernate.path",
"hibernate.cfg.xml");
- if ((path!=null)&&((new File(path).exists() ||
context.getEngineContext() == null))) {
- setPath (path);
+ if ((path != null) && ((new File(path).exists() ||
context.getEngineContext() == null))) {
+ setPath(path);
} else {
try {
setHibUrl(context.getEngineContext().getResource(path));
@@ -69,43 +78,52 @@
}
/**
- * Initialize the storage engine with a specific path
- * This is used for tests.
+ * Initialize the storage engine with a specific path This is used for
tests.
+ *
* @param hibpath
*/
- public XWikiHibernateBaseStore(String hibpath) {
+ public XWikiHibernateBaseStore(String hibpath)
+ {
setPath(hibpath);
}
/**
* Allows to get the current hibernate config file path
+ *
* @return
*/
- public String getPath() {
+ public String getPath()
+ {
return hibpath;
}
/**
* Allows to set the current hibernate config file path
+ *
* @param hibpath
*/
- public void setPath(String hibpath) {
+ public void setPath(String hibpath)
+ {
this.hibpath = hibpath;
}
/**
* Get's the hibernate config path as an URL
+ *
* @return
*/
- public URL getHibUrl() {
+ public URL getHibUrl()
+ {
return hiburl;
}
/**
* Set the hibernate config path as an URL
+ *
* @param hiburl
*/
- public void setHibUrl(URL hiburl) {
+ public void setHibUrl(URL hiburl)
+ {
this.hiburl = hiburl;
}
@@ -125,16 +143,18 @@
/**
* Allows to init the hibernate configuration
+ *
* @throws org.hibernate.HibernateException
*/
- private void initHibernate() throws HibernateException {
+ private void initHibernate() throws HibernateException
+ {
// Load Configuration and build SessionFactory
String path = getPath();
- if (path!=null)
+ if (path != null)
setConfiguration((new Configuration()).configure(new File(path)));
else {
URL hiburl = getHibUrl();
- if (hiburl!=null)
+ if (hiburl != null)
setConfiguration(new Configuration().configure(hiburl));
else
setConfiguration(new Configuration().configure());
@@ -144,110 +164,122 @@
}
/**
- * This get's the current session.
- * This is set in beginTransaction
+ * This get's the current session. This is set in beginTransaction
+ *
* @param context
* @return
*/
- public Session getSession(XWikiContext context) {
+ public Session getSession(XWikiContext context)
+ {
Session session = (Session) context.get("hibsession");
// Make sure we are in this mode
- try {
- if (session!=null) {
- session.setFlushMode(FlushMode.COMMIT);
- }
- } catch (org.hibernate.SessionException ex) {
- session = null;
- }
+ try {
+ if (session != null) {
+ session.setFlushMode(FlushMode.COMMIT);
+ }
+ } catch (org.hibernate.SessionException ex) {
+ session = null;
+ }
return session;
}
/**
- * Allows to set the current session in the context
- * This is set in beginTransaction
+ * Allows to set the current session in the context This is set in
beginTransaction
+ *
* @param session
* @param context
*/
- public void setSession(Session session, XWikiContext context) {
- if (session==null)
+ public void setSession(Session session, XWikiContext context)
+ {
+ if (session == null)
context.remove("hibsession");
else
context.put("hibsession", session);
}
-
/**
- * Allows to get the current transaction from the context
- * This is set in beginTransaction
+ * Allows to get the current transaction from the context This is set in
beginTransaction
+ *
* @param context
* @return
*/
- public Transaction getTransaction(XWikiContext context) {
+ public Transaction getTransaction(XWikiContext context)
+ {
Transaction transaction = (Transaction) context.get("hibtransaction");
return transaction;
}
/**
- * Allows to set the current transaction
- * This is set in beginTransaction
+ * Allows to set the current transaction This is set in beginTransaction
+ *
* @param transaction
* @param context
*/
- public void setTransaction(Transaction transaction, XWikiContext context) {
- if (transaction==null)
+ public void setTransaction(Transaction transaction, XWikiContext context)
+ {
+ if (transaction == null)
context.remove("hibtransaction");
else
context.put("hibtransaction", transaction);
}
-
/**
- * Allows to shut down the hibernate configuration
- * Closing all pools and connections
+ * Allows to shut down the hibernate configuration Closing all pools and
connections
+ *
* @param context
* @throws HibernateException
*/
- public void shutdownHibernate(XWikiContext context) throws
HibernateException {
+ public void shutdownHibernate(XWikiContext context) throws
HibernateException
+ {
Session session = getSession(context);
preCloseSession(session);
closeSession(session);
- if (getSessionFactory()!=null) {
-
((SessionFactoryImpl)getSessionFactory()).getConnectionProvider().close();
+ if (getSessionFactory() != null) {
+ ((SessionFactoryImpl)
getSessionFactory()).getConnectionProvider().close();
}
}
/**
* Allows to update the schema to match the hibernate mapping
+ *
* @param context
* @throws HibernateException
*/
- public void updateSchema(XWikiContext context) throws HibernateException {
+ public void updateSchema(XWikiContext context) throws HibernateException
+ {
updateSchema(context, false);
}
/**
* Allows to update the schema to match the hibernate mapping
+ *
* @param context
* @param force defines wether or not to force the update despite the
xwiki.cfg settings
* @throws HibernateException
*/
- public synchronized void updateSchema(XWikiContext context, boolean force)
throws HibernateException {
+ public synchronized void updateSchema(XWikiContext context, boolean force)
+ throws HibernateException
+ {
// We don't update the schema if the XWiki hibernate config parameter
says not to update
- if ((!force) && (context.getWiki() != null) &&
("0".equals(context.getWiki().Param("xwiki.store.hibernate.updateschema")))) {
+ if ((!force) && (context.getWiki() != null)
+ &&
("0".equals(context.getWiki().Param("xwiki.store.hibernate.updateschema")))) {
if (log.isDebugEnabled())
log.debug("Schema update deactivated for wiki " +
context.getDatabase());
return;
}
-
+
if (log.isInfoEnabled()) {
log.info("Updating schema update for wiki " +
context.getDatabase() + " ...");
}
try {
- String fullName =
((context!=null)&&(context.getWiki()!=null)&&(context.getWiki().isMySQL())) ?
"concat('xwd_web','.','xwd_name)" : "xwd_fullname";
+ String fullName =
+ ((context != null) && (context.getWiki() != null) &&
(context.getWiki().isMySQL()))
+ ? "concat('xwd_web','.','xwd_name)" : "xwd_fullname";
String[] schemaSQL = getSchemaUpdateScript(getConfiguration(),
context);
- String[] addSQL = {
+ String[] addSQL =
+ {
// Make sure we have no null valued in integer fields
"update xwikidoc set xwd_translation=0 where xwd_translation
is null",
"update xwikidoc set xwd_language='' where xwd_language is
null",
@@ -255,17 +287,16 @@
"update xwikidoc set xwd_fullname=" + fullName + " where
xwd_fullname is null",
"update xwikidoc set xwd_elements=3 where xwd_elements is
null",
"delete from xwikiproperties where xwp_name like 'editbox_%'
and xwp_classtype='com.xpn.xwiki.objects.LongProperty'",
- "delete from xwikilongs where xwl_name like 'editbox_%'"
- };
+ "delete from xwikilongs where xwl_name like 'editbox_%'"};
- int inb = (schemaSQL==null) ? 0 : schemaSQL.length;
+ int inb = (schemaSQL == null) ? 0 : schemaSQL.length;
int nb = inb + addSQL.length;
String[] sql = new String[nb];
- if (schemaSQL!=null) {
- for (int i=0;i<inb;i++)
+ if (schemaSQL != null) {
+ for (int i = 0; i < inb; i++)
sql[i] = schemaSQL[i];
}
- for (int i=0;i<addSQL.length;i++)
+ for (int i = 0; i < addSQL.length; i++)
sql[i + inb] = addSQL[i];
updateSchema(sql, context);
@@ -293,8 +324,8 @@
DatabaseProduct databaseProduct = getDatabaseProductName(context);
if (databaseProduct == DatabaseProduct.DERBY) {
- return wikiName.equalsIgnoreCase(context.getMainXWiki()) ? "APP" :
wikiName
- .replace('-', '_');
+ return wikiName.equalsIgnoreCase(context.getMainXWiki()) ? "APP" :
wikiName.replace(
+ '-', '_');
} else if (databaseProduct == DatabaseProduct.HSQLDB) {
return wikiName.equalsIgnoreCase(context.getMainXWiki()) ?
"PUBLIC" : wikiName
.replace('-', '_');
@@ -341,12 +372,13 @@
session = getSession(context);
connection = session.connection();
setDatabase(session, context);
-
+
String contextSchema = getSchemaFromWikiName(context);
DatabaseProduct databaseProduct = getDatabaseProductName(context);
if (databaseProduct == DatabaseProduct.ORACLE
- || databaseProduct == DatabaseProduct.HSQLDB ||
databaseProduct == DatabaseProduct.DERBY) {
+ || databaseProduct == DatabaseProduct.HSQLDB
+ || databaseProduct == DatabaseProduct.DERBY) {
dschema = config.getProperty(Environment.DEFAULT_SCHEMA);
config.setProperty(Environment.DEFAULT_SCHEMA, contextSchema);
Iterator iter = config.getTableMappings();
@@ -376,22 +408,24 @@
} catch (Exception e) {
}
}
-
+
return schemaSQL;
}
/**
* Runs the update script on the current database
+ *
* @param createSQL
* @param context
*/
- public void updateSchema(String[] createSQL, XWikiContext context) {
+ public void updateSchema(String[] createSQL, XWikiContext context)
+ {
// Updating the schema for custom mappings
Session session;
Connection connection;
- Statement stmt=null;
+ Statement stmt = null;
boolean bTransaction = true;
- MonitorPlugin monitor = Util.getMonitorPlugin(context);
+ MonitorPlugin monitor = Util.getMonitorPlugin(context);
String sql = "";
try {
@@ -402,54 +436,59 @@
stmt = connection.createStatement();
// Start monitoring timer
- if (monitor!=null)
+ if (monitor != null)
monitor.startTimer("sqlupgrade");
for (int j = 0; j < createSQL.length; j++) {
sql = createSQL[j];
- if ( log.isDebugEnabled() ) log.debug("Update Schema sql: " +
sql);
+ if (log.isDebugEnabled())
+ log.debug("Update Schema sql: " + sql);
stmt.executeUpdate(sql);
}
connection.commit();
- }
- catch (Exception e) {
- if ( log.isErrorEnabled() ) {
+ } catch (Exception e) {
+ if (log.isErrorEnabled()) {
log.error("Failed updating schema while executing query \"" +
sql + "\"", e);
}
- }
- finally {
+ } finally {
try {
- if (stmt!=null) stmt.close();
- } catch (Exception e) {};
+ if (stmt != null)
+ stmt.close();
+ } catch (Exception e) {
+ }
+ ;
try {
if (bTransaction)
endTransaction(context, true);
- } catch (Exception e) {}
+ } catch (Exception e) {
+ }
// End monitoring timer
- if (monitor!=null)
+ if (monitor != null)
monitor.endTimer("sqlupgrade");
}
}
-
/**
- * Custom Mapping
- * This function update the schema based on the dynamic custom mapping
- * provided by the class
+ * Custom Mapping This function update the schema based on the dynamic
custom mapping provided
+ * by the class
+ *
* @param bclass
* @param context
* @throws com.xpn.xwiki.XWikiException
*/
- public void updateSchema(BaseClass bclass, XWikiContext context) throws
XWikiException {
+ public void updateSchema(BaseClass bclass, XWikiContext context) throws
XWikiException
+ {
String custommapping = bclass.getCustomMapping();
if (!bclass.hasExternalCustomMapping())
- return;
+ return;
Configuration config = makeMapping(bclass.getName(), custommapping);
- /* if (isValidCustomMapping(bclass.getName(), config, bclass)==false) {
- throw new XWikiException( XWikiException.MODULE_XWIKI_STORE,
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_INVALID_MAPPING,
- "Cannot update schema for class " + bclass.getName() + "
because of an invalid mapping");
- } */
+ /*
+ * if (isValidCustomMapping(bclass.getName(), config, bclass)==false)
{ throw new
+ * XWikiException( XWikiException.MODULE_XWIKI_STORE,
+ * XWikiException.ERROR_XWIKI_STORE_HIBERNATE_INVALID_MAPPING, "Cannot
update schema for
+ * class " + bclass.getName() + " because of an invalid mapping"); }
+ */
String[] sql = getSchemaUpdateScript(config, context);
updateSchema(sql, context);
@@ -457,29 +496,33 @@
/**
* Initializes hibernate and calls updateSchema if necessary
+ *
* @param context
* @throws HibernateException
*/
- public void checkHibernate(XWikiContext context) throws HibernateException
{
+ public void checkHibernate(XWikiContext context) throws HibernateException
+ {
- if (getSessionFactory()==null) {
+ if (getSessionFactory() == null) {
initHibernate();
/* Check Schema */
- if (getSessionFactory()!=null) {
+ if (getSessionFactory() != null) {
updateSchema(context);
}
}
}
/**
- * Checks if this xwiki setup is virtual
- * meaning if multiple wikis can be accessed using the same database pool
+ * Checks if this xwiki setup is virtual meaning if multiple wikis can be
accessed using the
+ * same database pool
+ *
* @param context
* @return
*/
- protected boolean isVirtual(XWikiContext context) {
- if ((context==null)||(context.getWiki()==null))
+ protected boolean isVirtual(XWikiContext context)
+ {
+ if ((context == null) || (context.getWiki() == null))
return true;
return context.getWiki().isVirtual();
}
@@ -497,10 +540,10 @@
try {
if (log.isDebugEnabled())
log.debug("Switch database to: " + context.getDatabase());
-
+
if (context.getDatabase() != null) {
String schemaName = getSchemaFromWikiName(context);
-
+
DatabaseProduct databaseProduct =
getDatabaseProductName(context);
if (DatabaseProduct.ORACLE == databaseProduct) {
Statement stmt = null;
@@ -558,38 +601,47 @@
/**
* Begins a transaction
+ *
* @param context
* @return
* @throws XWikiException
*/
- public boolean beginTransaction(XWikiContext context) throws
XWikiException {
- return beginTransaction(null, true, context);
+ public boolean beginTransaction(XWikiContext context) throws XWikiException
+ {
+ return beginTransaction(null, true, context);
}
/**
* Begins a transaction
+ *
* @param withTransaction
* @param context
* @return
* @throws XWikiException
*/
- public boolean beginTransaction(boolean withTransaction, XWikiContext
context) throws XWikiException {
- return beginTransaction(null, withTransaction, context);
+ public boolean beginTransaction(boolean withTransaction, XWikiContext
context)
+ throws XWikiException
+ {
+ return beginTransaction(null, withTransaction, context);
}
/**
* Begins a transaction with a specific SessionFactory
+ *
* @param sfactory
* @param context
* @return
* @throws XWikiException
*/
- public boolean beginTransaction(SessionFactory sfactory, XWikiContext
context) throws XWikiException {
+ public boolean beginTransaction(SessionFactory sfactory, XWikiContext
context)
+ throws XWikiException
+ {
return beginTransaction(sfactory, true, context);
}
/**
* Begins a transaction with a specific SessionFactory
+ *
* @param sfactory
* @param withTransaction
* @param context
@@ -597,32 +649,40 @@
* @throws HibernateException
* @throws XWikiException
*/
- public boolean beginTransaction(SessionFactory sfactory, boolean
withTransaction, XWikiContext context)
- throws HibernateException, XWikiException {
+ public boolean beginTransaction(SessionFactory sfactory, boolean
withTransaction,
+ XWikiContext context) throws HibernateException, XWikiException
+ {
Transaction transaction = getTransaction(context);
Session session = getSession(context);
- if (((session==null)&&(transaction!=null))
- ||((transaction==null)&&(session!=null))) {
- if ( log.isWarnEnabled() ) log.warn("Incompatible session (" +
session + ") and transaction (" + transaction + ") status");
+ if (((session == null) && (transaction != null))
+ || ((transaction == null) && (session != null))) {
+ if (log.isWarnEnabled())
+ log.warn("Incompatible session (" + session + ") and
transaction (" + transaction
+ + ") status");
+ // TODO: Fix this problem, don't ignore it!
return false;
}
- if (session!=null) {
- if ( log.isDebugEnabled() ) log.debug("Taking session from context
" + session);
- if ( log.isDebugEnabled() ) log.debug("Taking transaction from
context " + transaction);
+ if (session != null) {
+ if (log.isDebugEnabled())
+ log.debug("Taking session from context " + session);
+ if (log.isDebugEnabled())
+ log.debug("Taking transaction from context " + transaction);
return false;
}
- if (session==null) {
- if ( log.isDebugEnabled() ) log.debug("Trying to get session from
pool");
- if (sfactory==null)
- session = (SessionImpl)getSessionFactory().openSession();
+ if (session == null) {
+ if (log.isDebugEnabled())
+ log.debug("Trying to get session from pool");
+ if (sfactory == null)
+ session = (SessionImpl) getSessionFactory().openSession();
else
session = sfactory.openSession();
- if ( log.isDebugEnabled() ) log.debug("Taken session from pool " +
session);
+ if (log.isDebugEnabled())
+ log.debug("Taken session from pool " + session);
// Keep some statistics about session and connections
nbConnections++;
@@ -631,11 +691,13 @@
setSession(session, context);
setDatabase(session, context);
- if ( log.isDebugEnabled() )
+ if (log.isDebugEnabled()) {
log.debug("Trying to open transaction");
+ }
transaction = session.beginTransaction();
- if ( log.isDebugEnabled() )
+ if (log.isDebugEnabled()) {
log.debug("Opened transaction " + transaction);
+ }
setTransaction(transaction, context);
}
return true;
@@ -643,47 +705,53 @@
/**
* Adding a connection to the Monitor module
+ *
* @param connection
* @param context
* @todo This function is temporarily deactivated because of an error that
causes memory leaks.
*/
- private void addConnection(Connection connection, XWikiContext context) {
-/* if (connection!=null)
- connections.put(connection, new ConnectionMonitor(connection,
context));
-*/ }
+ private void addConnection(Connection connection, XWikiContext context)
+ {
+ /*
+ * if (connection!=null) connections.put(connection, new
ConnectionMonitor(connection,
+ * context));
+ */}
/**
* Remove a connection to the Monitor module
+ *
* @param connection
* @todo This function is temporarily deactivated because of an error that
causes memory leaks.
*/
- private void removeConnection(Connection connection) {
-// connection.equals(connection) = false for some strange reasons.
-/* try {
- if (connection!=null)
- connections.remove(connection);
- } catch (Exception e) {
- }
-*/ }
+ private void removeConnection(Connection connection)
+ {
+ // connection.equals(connection) = false for some strange reasons.
+ /*
+ * try { if (connection!=null) connections.remove(connection); } catch
(Exception e) { }
+ */}
/**
* Ends a transaction
+ *
* @param context
* @param commit should we commit or not
*/
- public void endTransaction(XWikiContext context, boolean commit) {
+ public void endTransaction(XWikiContext context, boolean commit)
+ {
endTransaction(context, commit, false);
}
/**
* Ends a transaction
+ *
* @param context
* @param commit should we commit or not
* @param withTransaction
* @throws HibernateException
*/
public void endTransaction(XWikiContext context, boolean commit, boolean
withTransaction)
- throws HibernateException {
+ throws HibernateException
+ {
Session session = null;
try {
session = getSession(context);
@@ -691,12 +759,13 @@
setSession(null, context);
setTransaction(null, context);
- if (transaction!=null) {
+ if (transaction != null) {
// We need to clean up our connection map first because the
connection will
// be aggressively closed by hibernate 3.1 and more
preCloseSession(session);
- if ( log.isDebugEnabled() ) log.debug("Releasing hibernate
transaction " + transaction);
+ if (log.isDebugEnabled())
+ log.debug("Releasing hibernate transaction " +
transaction);
if (commit) {
transaction.commit();
} else {
@@ -704,32 +773,36 @@
}
}
} finally {
- closeSession(session);
+ closeSession(session);
}
}
/**
* Closes the hibernate session
+ *
* @param session
* @throws HibernateException
*/
- private void closeSession(Session session) throws HibernateException {
- if (session!=null) {
- session.close();
+ private void closeSession(Session session) throws HibernateException
+ {
+ if (session != null) {
+ session.close();
}
}
-
/**
* Closes the hibernate session
+ *
* @param session
* @throws HibernateException
*/
- private void preCloseSession(Session session) throws HibernateException {
- if (session!=null) {
- if ( log.isDebugEnabled() ) log.debug("Releasing hibernate session
" + session);
+ private void preCloseSession(Session session) throws HibernateException
+ {
+ if (session != null) {
+ if (log.isDebugEnabled())
+ log.debug("Releasing hibernate session " + session);
Connection connection = getRealConnection(session);
- if ((connection!=null)) {
+ if ((connection != null)) {
nbConnections--;
try {
removeConnection(connection);
@@ -741,153 +814,182 @@
}
}
- /* Hack to get the real JDBC connection because hibernate 3.1 wraps the
connection in a proxy and this creates a memory leak */
- private Connection getRealConnection(Session session) {
+ /*
+ * Hack to get the real JDBC connection because hibernate 3.1 wraps the
connection in a proxy
+ * and this creates a memory leak
+ */
+ private Connection getRealConnection(Session session)
+ {
Connection conn = session.connection();
- if (conn instanceof Proxy) {
+ if (conn instanceof Proxy) {
Object bcp = Proxy.getInvocationHandler(conn);
if (bcp instanceof BorrowedConnectionProxy) {
- ConnectionManager cm = (ConnectionManager)
XWiki.getPrivateField(bcp, "connectionManager");
- if (cm!=null)
- return cm.getConnection();
+ ConnectionManager cm =
+ (ConnectionManager) XWiki.getPrivateField(bcp,
"connectionManager");
+ if (cm != null)
+ return cm.getConnection();
}
}
return conn;
}
/**
- * Cleanup all sessions
- * Used at the shutdown time
+ * Cleanup all sessions Used at the shutdown time
+ *
* @param context
*/
- public void cleanUp(XWikiContext context) {
+ public void cleanUp(XWikiContext context)
+ {
try {
Session session = getSession(context);
- if (session!=null) {
- if ( log.isWarnEnabled() ) log.warn("Cleanup of session was
needed: " + session);
+ if (session != null) {
+ if (log.isWarnEnabled())
+ log.warn("Cleanup of session was needed: " + session);
endTransaction(context, false);
}
} catch (HibernateException e) {
}
}
- public SessionFactory getSessionFactory() {
+ public SessionFactory getSessionFactory()
+ {
return sessionFactory;
}
- public void setSessionFactory(SessionFactory sessionFactory) {
+ public void setSessionFactory(SessionFactory sessionFactory)
+ {
this.sessionFactory = sessionFactory;
}
- public Configuration getConfiguration() {
+ public Configuration getConfiguration()
+ {
return configuration;
}
- public void setConfiguration(Configuration configuration) {
+ public void setConfiguration(Configuration configuration)
+ {
this.configuration = configuration;
}
- public Collection getConnections() {
+ public Collection getConnections()
+ {
return connections.values();
}
- public int getNbConnections() {
+ public int getNbConnections()
+ {
return nbConnections;
}
- public void setNbConnections(int nbConnections) {
+ public void setNbConnections(int nbConnections)
+ {
this.nbConnections = nbConnections;
}
+ public class ConnectionMonitor
+ {
+ private Exception exception;
- public class ConnectionMonitor {
- private Exception exception;
private Connection connection;
+
private Date date;
+
private URL url = null;
- public ConnectionMonitor(Connection connection, XWikiContext context) {
+ public ConnectionMonitor(Connection connection, XWikiContext context)
+ {
this.setConnection(connection);
try {
setDate(new Date());
setException(new XWikiException());
XWikiRequest request = context.getRequest();
- if (request!=null)
+ if (request != null)
setURL(XWiki.getRequestURL(context.getRequest()));
} catch (Throwable e) {
}
}
- public Connection getConnection() {
+ public Connection getConnection()
+ {
return connection;
}
- public void setConnection(Connection connection) {
+ public void setConnection(Connection connection)
+ {
this.connection = connection;
}
- public Date getDate() {
+ public Date getDate()
+ {
return date;
}
- public void setDate(Date date) {
+ public void setDate(Date date)
+ {
this.date = date;
}
- public Exception getException() {
+ public Exception getException()
+ {
return exception;
}
- public void setException(Exception exception) {
+ public void setException(Exception exception)
+ {
this.exception = exception;
}
- public URL getURL() {
+ public URL getURL()
+ {
return url;
}
- public void setURL(URL url) {
+ public void setURL(URL url)
+ {
this.url = url;
}
}
- protected Configuration makeMapping(String className, String
custommapping1) {
+ protected Configuration makeMapping(String className, String
custommapping1)
+ {
Configuration hibconfig = new Configuration();
{
- hibconfig.addXML(makeMapping(className , "xwikicustom_" +
className.replaceAll("\\.", "_"), custommapping1));
+ hibconfig.addXML(makeMapping(className, "xwikicustom_"
+ + className.replaceAll("\\.", "_"), custommapping1));
}
hibconfig.buildMappings();
return hibconfig;
}
- protected String makeMapping(String entityName, String tableName, String
custommapping1) {
- String custommapping = "<?xml version=\"1.0\"?>\n" +
- "<!DOCTYPE hibernate-mapping PUBLIC\n" +
- "\t\"-//Hibernate/Hibernate Mapping DTD//EN\"\n" +
-
"\t\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
- "<hibernate-mapping>" +
- "<class entity-name=\"" + entityName + "\" table=\"" +
tableName+ "\">\n" +
- " <id name=\"id\" type=\"integer\" unsaved-value=\"any\">\n" +
- " <column name=\"XWO_ID\" not-null=\"true\" />\n" +
- " <generator class=\"assigned\" />\n" +
- " </id>\n" +
- custommapping1 +
- "</class>\n" +
- "</hibernate-mapping>";
+ protected String makeMapping(String entityName, String tableName, String
custommapping1)
+ {
+ String custommapping =
+ "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE hibernate-mapping
PUBLIC\n"
+ + "\t\"-//Hibernate/Hibernate Mapping DTD//EN\"\n"
+ +
"\t\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n"
+ + "<hibernate-mapping>" + "<class entity-name=\"" + entityName
+ "\" table=\""
+ + tableName + "\">\n"
+ + " <id name=\"id\" type=\"integer\" unsaved-value=\"any\">\n"
+ + " <column name=\"XWO_ID\" not-null=\"true\" />\n"
+ + " <generator class=\"assigned\" />\n" + " </id>\n" +
custommapping1
+ + "</class>\n" + "</hibernate-mapping>";
return custommapping;
}
/**
* Callback (closure) interface for operations in hibernate. spring like.
*/
- public interface HibernateCallback {
+ public interface HibernateCallback
+ {
/**
* method executed by [EMAIL PROTECTED] XWikiHibernateBaseStore} and
pass open session to it.
+ *
* @param session - open hibernate session
- * @return any you need be returned by [EMAIL PROTECTED]
XWikiHibernateBaseStore#execute(XWikiContext, boolean, boolean,
HibernateCallback)}
+ * @return any you need be returned by
+ * [EMAIL PROTECTED]
XWikiHibernateBaseStore#execute(XWikiContext, boolean, boolean,
HibernateCallback)}
* @throws HibernateException if any store specific exception
* @throws XWikiException if exception in xwiki.
*/
@@ -895,37 +997,42 @@
}
/**
- * Execute method for operations in hibernate. spring like.
+ * Execute method for operations in hibernate. spring like.
+ *
* @return [EMAIL PROTECTED] HibernateCallback#doInHibernate(Session)}
* @param context - used everywhere.
* @param bTransaction - should store use old transaction(false) or create
new (true)
* @param doCommit - should store commit changes(if any), or rollback it.
- * @param cb - callback to execute
+ * @param cb - callback to execute
* @throws XWikiException if any error
*/
- public Object execute(XWikiContext context, boolean bTransaction, boolean
doCommit, HibernateCallback cb) throws XWikiException {
+ public Object execute(XWikiContext context, boolean bTransaction, boolean
doCommit,
+ HibernateCallback cb) throws XWikiException
+ {
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
// Start monitoring timer
- if (monitor!=null)
+ if (monitor != null)
monitor.startTimer("hibernate");
-
+
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
}
-
+
return cb.doInHibernate(getSession(context));
} catch (Exception e) {
if (e instanceof XWikiException)
- throw (XWikiException)e;
- throw new XWikiException( XWikiException.MODULE_XWIKI_STORE,
XWikiException.ERROR_XWIKI_UNKNOWN,
- "Exception while hibernate execute", e);
+ throw (XWikiException) e;
+ throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
+ XWikiException.ERROR_XWIKI_UNKNOWN,
+ "Exception while hibernate execute",
+ e);
} finally {
try {
if (bTransaction)
endTransaction(context, doCommit);
- if (monitor!=null)
+ if (monitor != null)
monitor.endTimer("hibernate");
} catch (Exception e) {
if (log.isErrorEnabled())
@@ -935,28 +1042,36 @@
}
/**
- * Execute method for read-only operations in hibernate. spring like.
+ * Execute method for read-only operations in hibernate. spring like.
+ *
* @return [EMAIL PROTECTED] HibernateCallback#doInHibernate(Session)}
* @param context - used everywhere.
* @param bTransaction - should store to use old transaction(false) or
create new (true)
- * @param cb - callback to execute
+ * @param cb - callback to execute
* @throws XWikiException if any error
- * @see #execute(XWikiContext, boolean, boolean,
com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)
+ * @see #execute(XWikiContext, boolean, boolean,
+ * com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)
*/
- public Object executeRead(XWikiContext context, boolean bTransaction,
HibernateCallback cb) throws XWikiException {
+ public Object executeRead(XWikiContext context, boolean bTransaction,
HibernateCallback cb)
+ throws XWikiException
+ {
return execute(context, bTransaction, false, cb);
}
/**
- * Execute method for read-write operations in hibernate. spring like.
+ * Execute method for read-write operations in hibernate. spring like.
+ *
* @return [EMAIL PROTECTED] HibernateCallback#doInHibernate(Session)}
* @param context - used everywhere.
* @param bTransaction - should store to use old transaction(false) or
create new (true)
- * @param cb - callback to execute
+ * @param cb - callback to execute
* @throws XWikiException if any error
- * @see #execute(XWikiContext, boolean, boolean,
com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)
+ * @see #execute(XWikiContext, boolean, boolean,
+ * com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)
*/
- public Object executeWrite(XWikiContext context, boolean bTransaction,
HibernateCallback cb) throws XWikiException {
+ public Object executeWrite(XWikiContext context, boolean bTransaction,
HibernateCallback cb)
+ throws XWikiException
+ {
return execute(context, bTransaction, true, cb);
}
}
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications