Hi all,
We have a lot of problems with concurrent execution of a single service running that query data using sessions (org.hibernate.SessionFactory) builded dynamically with hibernate, the problem is when th program execute the concurrency way the last request works and the first requests throw a runtime error with the access data.
How do we make concurrent executions on the same servlet without error?This method build the session:
Code:
public static synchronized void buildSession(Config configBD)
{
String configFile = new String();
try
{
if(sessionFactoryMap.get(getKeyURL()) == null)
{
configFile = configBD.getCadena();
configuration = new Configuration();
configuration.configure(configFile);
configuration.setProperty("hibernate.connection.url", getKeyURL());
//configuration.setProperty("show_sql","true");
sessionFactory = configuration.buildSessionFactory();
sessionFactoryMap.put(getKeyURL(), sessionFactory);
}
else
{
sessionFactory = (SessionFactory) sessionFactoryMap.get(getKeyURL());
}
}
catch (Exception e)
{
new InformableException(e.getMessage());
e.printStackTrace();
}
}
this method return the current session of the sessionFactory
Code:
public static Session currentSession(String key) throws HibernateException
{
HashMap sessionMaps = (HashMap) sessionMapsThreadLocal.get();
if (sessionMaps == null)
{
sessionMaps = new HashMap();
sessionMapsThreadLocal.set(sessionMaps);
}
// Open a new Session, if this Thread has none yet
Session s = (Session) sessionMaps.get(key);
if (s == null)
{
s = ((SessionFactory) sessionFactoryMap.get(key)).openSession();
sessionMaps.put(key, s);
}
return s;
}
This method return the session:
Code:
protected Session getSession()
{
Session s = (Session) sessionThreadLocal.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = HibernateUtil.currentSession(HibernateUtil.getKeyURL());
// Store it in the ThreadLocal variable
sessionThreadLocal.set(s);
}
return s;
}
and that is the file configurator hibernate.cfg.xml:
Code:
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="hibernate.connection.url">jdbc:informix-sqli://192.168.41.32:1526/general:informixserver=dirtel;IFX_AUTOFREE=1;OPTOFC=1</property>
<property name="hibernate.connection.username">planpub</property>
<property name="hibernate.connection.password">pdf1029</property>
<property name="hibernate.dialect">org.hibernate.dialect.InformixDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<!--Hibernate built-in connection pool -->
<!--<property name="connection.pool_size">5</property>-->
<!--End Hibernate built-in connection pool -->
<!-- Configuration pool via c3p0-->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">150</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">0</property> <!-- seconds -->
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">1000</property> <!-- seconds -->
<property name="connection.autocommit">true</property>
<!-- End Configuration pool via c3p0-->
Please help us ....