簡體   English   中英

休眠配置3.3.2.GA管理的會話

[英]hibernate configuration 3.3.2.GA managed sessions

確保會話hibernate.cfg.xml 50個用戶的正確設置是什么? 在Hibernate 3.3.2.GA版本中,因為當連接5個以上的用戶時,我的應用程序無法響應對數據庫的某些請求。

好,這是配置文件

<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 -->

我通過這種方式將命令模式用於邏輯和控制會話

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;
}

作為@M。 Deinum說,您未關閉seesion對象。 transaction.commit(); 只會將更改提交到數據庫並刷新當前會話。

圍棋練習如下:

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;
   }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM