-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: MSSQL locking when doing multi transaction in one session
PostPosted: Tue Jun 30, 2009 12:21 am 
Newbie

Joined: Tue Feb 08, 2005 3:59 pm
Posts: 4
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>


Top
 Profile  
 
 Post subject: Re: MSSQL locking when doing multi transaction in one session
PostPosted: Tue Jun 30, 2009 12:34 am 
Newbie

Joined: Tue Feb 08, 2005 3:59 pm
Posts: 4
=== my mapping=====
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-update="true" lazy="true" name="beans.Menu"
optimistic-lock="all" table="menu">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property column="store_id" generated="never" lazy="false" length="19"
name="storeId" type="long"/>
<property column="menu_type" generated="never" lazy="false"
length="50" name="menuType" type="string"/>
<property column="description" generated="never" lazy="false"
length="100" name="description" type="string"/>
<property column="quantity" generated="never" lazy="false" length="10"
name="quantity" type="integer"/>
</class>
<class dynamic-update="true" lazy="true" name="beans.MenuModifiers"
optimistic-lock="all" table="menu_modifiers">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property column="store_id" generated="never" lazy="false" length="19"
name="storeId" type="long"/>
<property column="pregenerated" generated="never" lazy="false"
length="10" name="pregenerated" type="integer"/>
<property column="name" generated="never" lazy="false" length="50"
name="name" type="string"/>
<property column="description" generated="never" lazy="false"
length="50" name="description" type="string"/>
<property column="menu_id" generated="never" lazy="false" length="19"
name="menuId" type="long"/>
</class>
<class dynamic-update="true" lazy="true" name="beans.ModifierValues"
optimistic-lock="none" table="modifier_values">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property column="menu_modifier_id" generated="never" lazy="false"
length="19" name="menuModifierId" type="long"/>
<property column="name" generated="never" lazy="false" length="50"
name="name" type="string"/>
<property column="description" generated="never" lazy="false"
length="50" name="description" type="string"/>
<property column="code" generated="never" lazy="false" length="50"
name="code" type="string"/>
<property column="price" generated="never" lazy="false" length="19"
name="price" type="double"/>
<property column="quantity" generated="never" lazy="false" length="10"
name="quantity" type="integer"/>
</class>
<class dynamic-update="true" lazy="true" name="beans.Store"
optimistic-lock="none" table="store">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property column="corporation" generated="never" lazy="false"
length="50" name="corporation" type="string"/>
<property column="name" generated="never" lazy="false" length="50"
name="name" type="string"/>
<property column="phone" generated="never" lazy="false" length="11"
name="phone" type="string"/>
<property column="address" generated="never" lazy="false" length="50"
name="address" type="string"/>
<property column="tax" generated="never" lazy="false" length="19"
name="tax" type="double"/>
<property column="discount" generated="never" lazy="false" length="19"
name="discount" type="double"/>
<property column="grammar_file" generated="never" lazy="false"
length="50" name="grammarFile" type="string"/>
<property column="dtmf_grammar_file" generated="never" lazy="false"
length="50" name="dtmfGrammarFile" type="string"/>
<property column="dtmf_rule" generated="never" lazy="false"
length="50" name="dtmfRule" type="string"/>
<property column="codestore" generated="never" lazy="false"
length="19" name="codestore" type="long"/>
<property column="email" generated="never" lazy="false" length="255"
name="email" type="string"/>
<property column="password" generated="never" lazy="false"
length="255" name="password" type="string"/>
</class>
<class dynamic-update="true" lazy="true" name="beans.SynonymsModifiers"
optimistic-lock="dirty" table="synonyms_modifiers">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property column="modifier_value_id" generated="never" lazy="false"
length="19" name="modifierValueId" type="long"/>
<property column="name" generated="never" lazy="false" length="50"
name="name" type="string"/>
<property column="description" generated="never" lazy="false"
length="50" name="description" type="string"/>
<property column="code" generated="never" lazy="false" length="50"
name="code" type="string"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.