What is the proper settings to ensure sessions hibernate.cfg.xml 50 users? In the version of Hibernate 3.3.2.GA because when more than 5 users connect my application does not respond to some requests to the database.
OK, this is configuration file
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/scriniadb</property>
<property name="hibernate.connection.username">scrinia</property>
<property name="hibernate.connection.password">scrinia</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.default_schema">scrinia</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--Disable the second-level cache <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>Echo
all executed SQL to stdout -->
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!--<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="hibernate.validator.autoregister_listeners">false</property> -->
<property name="hibernate.max_fetch_depth">5</property>
<property name="hibernate.default_batch_fetch_size">10</property>
<property name="connection.autocommit">true</property>
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property> -->
<!--<property name="jboss.as.jpa.providerModule">org.hibernate:3</property> -->
<!-- Aqui van las property para hibernate search -->
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider
</property>
<property name="hibernate.search.default.indexBase">C:\indice\metadato</property>
<!--Mapping class -->
I use command pattern for logic and control sessions this way
public DataAccessCommand executeCommand(DataAccessCommand c) throws BusinessException {
// System.out.println("con transaccion");
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
c.setDAOFactory(daoFactory);
try {
c.execute();
} catch (BusinessException e) {
System.out.println("MESSAGE BUSINESS");
throw new BusinessException(e.getMessage(), e, e.getEnviarNotificacion());
} catch (RuntimeException e) {
System.out.println("MESSAGE RUNTIME " + e);
try {
if (transaction.isActive()) {
transaction.rollback();
}
throw new BusinessException(e, true);
} catch (RuntimeException ex2) {
Throwable exss = ex2;
while (exss.getCause() != null) {
exss = exss.getCause();
}
exss.initCause(e);
throw new BusinessException("No se pudo hacer un roll back de la transacción", ex2, true);
}
}
transaction.commit();
} catch (org.hibernate.exception.ConstraintViolationException e) {
if (transaction.isActive()) {
transaction.rollback();
}
HandlerException.ProcessException(e, e.getStackTrace()[0].toString(), "");
e.getSQL();
System.out.println("e.getSQL() " + e.getSQL());
e.getSQLException();
System.out.println("e.getSQLException()" + e.getSQLException());
System.out.println("e.getNextException()" + e.getSQLException().getNextException().getMessage());
e.getSQLException().getNextException().getStackTrace();
e.printStackTrace();
throw new BusinessException(e);
} catch (Exception ex) {
if (transaction.isActive()) {
transaction.rollback();
}
HandlerException.ProcessException(ex, ex.getStackTrace()[0].toString(), "");
System.err.println("Error on executed command e :" + ex.getMessage());
ex.printStackTrace();
throw new BusinessException(ex.getMessage());
}
return c;
}
As @M. Deinum said, you naver closed seesion
object. transaction.commit();
will only commit the changes to DB and flush the current session.
Go practice is as follows :
try
{
Session session = HibernateHrlper.getSession();
Transaction transaction = session.beginTransaction();
// Do your Work here
session.save(object);
transaction.commit();
}
catch(Exception e)
{
if(transaction !=null)
{
transaction.rollback();
}
}
finally
{
if(session !=null && session.isOpen())
{
session.close();
session = null;
}
if(transaction !=null)
{
transaction = null;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.