[jboss-svn-commits] JBL Code SVN: r12401 - labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/persist.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jun 8 01:19:18 EDT 2007


Author: tcunning
Date: 2007-06-08 01:19:17 -0400 (Fri, 08 Jun 2007)
New Revision: 12401

Added:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/persist/HibernateConn.java
Log:
bug:JBESB-434
Commit HibernateConn.


Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/persist/HibernateConn.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/persist/HibernateConn.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/persist/HibernateConn.java	2007-06-08 05:19:17 UTC (rev 12401)
@@ -0,0 +1,158 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.helpers.persist;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.jboss.soa.esb.ConfigurationException;
+
+/**
+ * This class is a Hibernate EventListener which implements hooks to all the available
+ * hibernate events.   It's not used at the moment, but might be a way of augmenting  
+ * the Hibernate Interceptor hooks which are currently implemented.
+ * 
+ * @author <a href="mailto:tcunning at redhat.com">tcunning at redhat.com</a>
+ */
+public class HibernateConn
+{
+	protected final static Logger m_Logger = Logger.getLogger(HibernateConn.class);
+
+	private Session m_session = null;
+	private Transaction m_tx = null;
+
+	/**
+	 * Constructor.
+	 * 
+	 * @param f_cfgfile configuration file name
+	 */
+	public HibernateConn(String f_cfgfile)
+	{
+		Configuration cfg = new Configuration();
+		cfg.configure(f_cfgfile);
+		SessionFactory sf = null;
+		try {
+			sf = HibernateSessionFactory.getInstance(cfg);
+		} catch (ConfigurationException e) {
+			m_Logger.error("", e);
+		}
+		
+		m_session = sf.getCurrentSession();
+	}
+
+	/**
+	 * 
+	 */
+	public void commit() {
+		if (null != m_tx)
+		{
+			m_tx.commit();
+		}
+	}
+
+	/**
+	 * 
+	 */
+	public void rollback() {
+		if (null != m_tx)
+		{
+			m_tx.rollback();
+		}
+	}
+
+	/**
+	 * 
+	 */
+	public void release() {
+		if (null != m_tx)
+		{
+			try
+			{
+				m_tx.rollback();
+			} catch (Exception eRoll) {
+			}
+		}
+		m_tx = null;
+	} // __________________________________
+
+	public Transaction getTransaction() {
+		if (null == m_tx)
+		{
+			connect();
+		}
+		
+		return m_tx;
+	} // __________________________________
+
+	public Session getSession() {
+		return m_session;
+	}
+	
+	public List execQuery(Query f_query) {
+		List l = null;
+		Transaction tx = null;
+		try {
+			tx = getTransaction();
+			l = f_query.list();
+			tx.commit();
+		} catch (Exception e) {
+			m_Logger.error("", e);
+			if (tx != null && tx.isActive()) {
+				tx.rollback();
+			}
+		}
+		return l;
+	}	
+	
+	private void connect() {
+		if (m_tx != null) {
+			return;
+		}
+
+		for (int i1 = 0; i1 < 5; i1++) {
+			try {
+				m_tx = m_session.beginTransaction();
+				break;
+			} catch (Exception e) {
+				m_Logger.debug("", e);
+			}
+			
+			try {
+				Thread.sleep(2000 + (new Double(100 * Math.random()))
+						.longValue());
+			} catch (InterruptedException ex) {
+				m_Logger.error("Thread interrupted.", ex);
+			}
+		}
+
+		if (m_tx == null)
+		{
+			throw new RuntimeException("connect() FAILED: no connection");
+		}
+	}
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list