<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: The Broken Database Connection</title>
	<link>http://www.techmasala.com/2007/01/02/the-broken-database-connection/</link>
	<description>TechMasala is a mixture of everything around information technology world. It is more for audience who want to keep in pace with what is happening in the internet world, for the newbies to the industry and know what foundations they should possess, some tips and tricks, latest in the web world.</description>
	<pubDate>Wed, 03 Dec 2008 23:44:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.1</generator>

	<item>
		<title>by: Thomas</title>
		<link>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-12150</link>
		<pubDate>Sun, 09 Sep 2007 13:18:59 +0000</pubDate>
		<guid>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-12150</guid>
					<description>Hello,

Thank you Very Muchn!!!
Your post was very helpfull to me!!!

Thanks again!
Thomas</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>Thank you Very Muchn!!!<br />
Your post was very helpfull to me!!!</p>
<p>Thanks again!<br />
Thomas
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Rashid</title>
		<link>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11533</link>
		<pubDate>Sat, 06 Jan 2007 16:04:24 +0000</pubDate>
		<guid>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11533</guid>
					<description>Hi Sridhar 
Wonderful fix for the bug. i have the same issue and i was so frustrated and right now i am instantiating my objects using conventional jdbc technique  i have the hibernate util class which i am using right now here it is
public class HibernateUtil { 
   private static String CONFIG_FILE_LOCATION = &quot;/hibernate.cfg.xml&quot;; 
   private static final ThreadLocal threadLocal = new ThreadLocal(); 
   private  static Configuration configuration = new Configuration(); 
   private static org.hibernate.SessionFactory sessionFactory; 
   private static String configFile = CONFIG_FILE_LOCATION; 
    static { 
       try { 
         configuration.configure(configFile); 
         sessionFactory = configuration.buildSessionFactory(); 
      } catch (Exception e) { 
         e.printStackTrace(); 
      } 
    } 
    private HibernateUtil() { 
    } 
    public static Session getSession() throws HibernateException { 
        Session session = (Session) threadLocal.get(); 

      if (session == null &amp;#124;&amp;#124; !session.isOpen()) { 
         if (sessionFactory == null) { 
            rebuildSessionFactory(); 
         } 
         session = (sessionFactory != null) ? sessionFactory.openSession() 
               : null; 
         threadLocal.set(session); 
      }
     try{ 
     final Transaction transaction = session.beginTransaction();
     session.createSQLQuery( &quot;select 1&quot; ).list();
     transaction.commit();
     }catch(GenericJDBCException){
       //check for sql state if 08S01 call get session again for 5 attempts
     }
     return session; 
    } 
   public synchronized static void rebuildSessionFactory() { 
      try { 
         configuration.configure(configFile); 
         sessionFactory = configuration.buildSessionFactory(); 
      } catch (Exception e) { 
         e.printStackTrace(); 
      } 
   } 
    public static void closeSession() throws HibernateException { 
        Session session = (Session) threadLocal.get(); 
        threadLocal.set(null); 

        if (session != null) { 
            session.close(); 
        } 
    } 

   public static org.hibernate.SessionFactory getSessionFactory() { 
      return sessionFactory; 
   } 

   public static void setConfigFile(String configFile) {       
      HibernateUtil.configFile = configFile; 
      sessionFactory = null; 
   } 
   public static Configuration getConfiguration() { 
      return configuration; 
   } 
 Should i be  catching GenericJDBCException in my openSesssion mehtod and recursively call open session again for five times. (i just added some comments not the acutal code yet) i am a bit confused since you code is  a bit differnt from mine. Please let me know fi you can guide me on this fix. Also since i want to test this on my windows environment how can i set the wait_timeout in mysql to a value around 10 minutes.  i tried to do it but still this error is not happening.
Thanks a bunch
Rashid</description>
		<content:encoded><![CDATA[<p>Hi Sridhar<br />
Wonderful fix for the bug. i have the same issue and i was so frustrated and right now i am instantiating my objects using conventional jdbc technique  i have the hibernate util class which i am using right now here it is<br />
public class HibernateUtil {<br />
   private static String CONFIG_FILE_LOCATION = &#8220;/hibernate.cfg.xml&#8221;;<br />
   private static final ThreadLocal threadLocal = new ThreadLocal();<br />
   private  static Configuration configuration = new Configuration();<br />
   private static org.hibernate.SessionFactory sessionFactory;<br />
   private static String configFile = CONFIG_FILE_LOCATION;<br />
    static {<br />
       try {<br />
         configuration.configure(configFile);<br />
         sessionFactory = configuration.buildSessionFactory();<br />
      } catch (Exception e) {<br />
         e.printStackTrace();<br />
      }<br />
    }<br />
    private HibernateUtil() {<br />
    }<br />
    public static Session getSession() throws HibernateException {<br />
        Session session = (Session) threadLocal.get(); </p>
<p>      if (session == null || !session.isOpen()) {<br />
         if (sessionFactory == null) {<br />
            rebuildSessionFactory();<br />
         }<br />
         session = (sessionFactory != null) ? sessionFactory.openSession()<br />
               : null;<br />
         threadLocal.set(session);<br />
      }<br />
     try{<br />
     final Transaction transaction = session.beginTransaction();<br />
     session.createSQLQuery( &#8220;select 1&#8243; ).list();<br />
     transaction.commit();<br />
     }catch(GenericJDBCException){<br />
       //check for sql state if 08S01 call get session again for 5 attempts<br />
     }<br />
     return session;<br />
    }<br />
   public synchronized static void rebuildSessionFactory() {<br />
      try {<br />
         configuration.configure(configFile);<br />
         sessionFactory = configuration.buildSessionFactory();<br />
      } catch (Exception e) {<br />
         e.printStackTrace();<br />
      }<br />
   }<br />
    public static void closeSession() throws HibernateException {<br />
        Session session = (Session) threadLocal.get();<br />
        threadLocal.set(null); </p>
<p>        if (session != null) {<br />
            session.close();<br />
        }<br />
    } </p>
<p>   public static org.hibernate.SessionFactory getSessionFactory() {<br />
      return sessionFactory;<br />
   } </p>
<p>   public static void setConfigFile(String configFile) {<br />
      HibernateUtil.configFile = configFile;<br />
      sessionFactory = null;<br />
   }<br />
   public static Configuration getConfiguration() {<br />
      return configuration;<br />
   }<br />
 Should i be  catching GenericJDBCException in my openSesssion mehtod and recursively call open session again for five times. (i just added some comments not the acutal code yet) i am a bit confused since you code is  a bit differnt from mine. Please let me know fi you can guide me on this fix. Also since i want to test this on my windows environment how can i set the wait_timeout in mysql to a value around 10 minutes.  i tried to do it but still this error is not happening.<br />
Thanks a bunch<br />
Rashid
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ramesh</title>
		<link>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11329</link>
		<pubDate>Fri, 05 Jan 2007 13:51:51 +0000</pubDate>
		<guid>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11329</guid>
					<description>Hi Giridhar,
You dont need to use Hibernate to fix this.
Just take a look at the code that is there in the post. All you have to do is pick up the exceptions wherever you are dealing with the connection.
Catch the GenericJDBCException and handle it the way its there in the code to reconnect and you must be set.
Let me know how it goes.
Ramesh</description>
		<content:encoded><![CDATA[<p>Hi Giridhar,<br />
You dont need to use Hibernate to fix this.<br />
Just take a look at the code that is there in the post. All you have to do is pick up the exceptions wherever you are dealing with the connection.<br />
Catch the GenericJDBCException and handle it the way its there in the code to reconnect and you must be set.<br />
Let me know how it goes.<br />
Ramesh
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Giridhar</title>
		<link>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11327</link>
		<pubDate>Fri, 05 Jan 2007 13:27:27 +0000</pubDate>
		<guid>http://www.techmasala.com/2007/01/02/the-broken-database-connection/#comment-11327</guid>
					<description>Hi,
I am facing the same problem as you've described. I've never used Hibernate. So how should I proceed? Do I need to use Hibernate just for this?  Excuse me if this is a very basic question. I have no idea what a Hibernate is!
Thanks for your time and help,
Giridhar</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I am facing the same problem as you&#8217;ve described. I&#8217;ve never used Hibernate. So how should I proceed? Do I need to use Hibernate just for this?  Excuse me if this is a very basic question. I have no idea what a Hibernate is!<br />
Thanks for your time and help,<br />
Giridhar
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
