I am trying to read and write to MSSQL 2005 express locks up. I have a filter that close the connection.
I am using hibernate 3.3.2.GA C3PO c3p0-0.9.1.2 Java 1.5.0_15
============== Hibernate sesssion ================== package hibernate;
import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.LockMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class HibernateSession {
private static Configuration configuration; private static SessionFactory sessionFactory; private static final ThreadLocal threadSession = new ThreadLocal(); private static final ThreadLocal threadTransaction = new ThreadLocal(); private static final ThreadLocal threadInterceptor = new ThreadLocal(); private static boolean debug = true;
/** * Returns the SessionFactory used for this static class. * * @return SessionFactory */ static { try { configuration = new Configuration(); sessionFactory = configuration.configure().buildSessionFactory(); // We could also let Hibernate bind it to JNDI: //configuration.configure().buildSessionFactory(); } catch (Exception ex) { // We have to catch Throwable, otherwise we will miss // NoClassDefFoundError and other subclasses of Error if(debug){ System.out.println("Building SessionFactory failed: " + ex.getMessage()); ex.printStackTrace(System.out); } } } public static SessionFactory getSessionFactory() { // Instead of a static variable, use JNDI: /*SessionFactory sessions = null; try { Context ctx = new InitialContext(); String jndiName = "jdbc/downloadsSQL"; sessions = (SessionFactory)ctx.lookup(jndiName); } catch (NamingException ex) { if(debug) ex.printStackTrace(System.out); } return sessions; */ return sessionFactory; }
/** * Returns the original Hibernate configuration. * * @return Configuration */ public static Configuration getConfiguration() { return configuration; }
/** * Rebuild the SessionFactory with the static Configuration. * */ public static void rebuildSessionFactory() throws Exception { synchronized(sessionFactory) { try { sessionFactory = getConfiguration().buildSessionFactory(); } catch (Exception ex) { throw new Exception(ex); } } }
/** * Rebuild the SessionFactory with the given Hibernate Configuration. * * @param cfg */ public static void rebuildSessionFactory(Configuration cfg) throws Exception { synchronized(sessionFactory) { try { sessionFactory = cfg.buildSessionFactory(); configuration = cfg; } catch (Exception ex) { throw new Exception(ex); } } }
/** * Retrieves the current Session local to the thread. * <p/> * If no Session is open, opens a new Session for the running thread. * * @return Session */ public static Session getSession() throws Exception { Session s = (Session) threadSession.get(); try { if (s == null) { if (getInterceptor() != null) { if(debug) System.out.println("Using interceptor: " + getInterceptor().getClass()); s = getSessionFactory().openSession(getInterceptor()); } else { s = getSessionFactory().openSession(); } threadSession.set(s); } } catch (HibernateException ex) { throw new Exception(ex); } return s; }
/** * Closes the Session local to the thread. */ @SuppressWarnings("unchecked") public static void closeSession() throws Exception { try { Session s = (Session) threadSession.get(); threadSession.set(null); if (s != null && s.isOpen()) { if(debug) System.out.println("Closing Session of this thread."); s.flush(); s.close(); } } catch (HibernateException ex) { throw new Exception(ex); } }
/** * Start a new database transaction. */ public static void beginTransaction() throws Exception { Transaction tx = (Transaction) threadTransaction.get(); try { if (tx == null) { if(debug) System.out.println("Starting new database transaction in this thread."); tx = getSession().beginTransaction(); threadTransaction.set(tx); } } catch (HibernateException ex) { throw new Exception(ex); } }
/** * Commit the database transaction. */ public static void commitTransaction() throws Exception { Transaction tx = (Transaction) threadTransaction.get(); try { if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) { if(debug) System.out.println("Committing database transaction of this thread."); tx.commit(); } threadTransaction.set(null); } catch (HibernateException ex) { rollbackTransaction(); throw new Exception(ex); } }
/** * Commit the database transaction. */ public static void rollbackTransaction() throws Exception { Transaction tx = (Transaction) threadTransaction.get(); try { threadTransaction.set(null); if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) { if(debug) System.out.println("Tyring to rollback database transaction of this thread."); tx.rollback(); } } catch (HibernateException ex) { throw new Exception(ex); } finally { closeSession(); } }
/** * Reconnects a Hibernate Session to the current Thread. * * @param session The Hibernate Session to be reconnected. */ public static void reconnect(Session session) throws Exception { try { session.reconnect(); threadSession.set(session); } catch (HibernateException ex) { throw new Exception(ex); } }
/** * Disconnect and return Session from current Thread. * * @return Session the disconnected Session */ public static Session disconnectSession()throws Exception {
Session session = getSession(); try { threadSession.set(null); if (session.isConnected() && session.isOpen()) session.disconnect(); } catch (HibernateException ex) { System.out.println("Error disconnecting"); } return session; }
/** * Register a Hibernate interceptor with the current thread. * <p> * Every Session opened is opened with this interceptor after * registration. Has no effect if the current Session of the * thread is already open, effective on next close()/getSession(). */ public static void registerInterceptor(Interceptor interceptor) { threadInterceptor.set(interceptor); }
private static Interceptor getInterceptor() { Interceptor interceptor = (Interceptor) threadInterceptor.get(); return interceptor; }
}
============== configuration file ==================== <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name=""> <property name="connection.username">user</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="connection.password">pass</property>
<!--com.microsoft.sqlserver.jdbc.SQLServerDriver --> <property name="connection.driver_class"> net.sourceforge.jtds.jdbc.Driver </property> <!-- jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=OrderTaker;--> <property name="connection.url"> jdbc:jtds:sqlserver://localhost/OrderTaker;instance=SQLEXPRESS </property>
<!-- Use the C3P0 connection pool provider --> <property name="show_sql">false</property> <property name="use_outer_join">true</property> <property name="jdbc.batch_size">0</property> <property name="hbm2ddl.auto">update</property> <property name="jdbc.use_scrollable_resultset">true</property> <property name="jdbc.use_streams_for_binary">true</property> <property name="connection.pool_size">20</property> <property name="c3p0.min_size">10</property> <property name="c3p0.max_size">100</property> <property name="c3p0.timeout">18000</property> <property name="c3p0.acquireRetryAttempts">30</property> <property name="c3p0.acquireIncrement">5</property> <property name="c3p0.automaticTestTable">zip</property> <property name="c3p0.idleConnectionTestPeriod">36000</property> <property name="c3p0.initialPoolSize">20</property> <property name="c3p0.maxPoolSize">100</property> <property name="c3p0.maxIdleTime">1200</property> <property name="c3p0.maxStatements">50</property> <property name="c3p0.minPoolSize">10</property>
<!-- Show and print nice SQL on stdout --> <property name="show_sql">faslse</property> <property name="format_sql">false</property> <property name="c3p0.autoCommitOnClose">true</property> <mapping resource="beans/mapping.hbm.xml" /> </session-factory> </hibernate-configuration>
|