Java Pool Hibernate c3p0

Asked

Viewed 167 times

1

Dear friends, good afternoon.

I have a Java solution that’s generating production problems, taking down the bank. After a few changes, I set up a pool using Hibernate c3p0 to manage the number of open sessions (maximum 100). The application respects the pool. It turns out that the method implemented is flawed: there is a queue that manages the sending of messages and, with each new message, a new session is opened for sending a single message.

My question: possible to establish a single session per user, to perform several transactions to send the message? And when there is transabordo for these transactions, open a new session per user? In the current configuration are opened 100 sessions, which are not closed and lock the sending of new messages.

Hibernate.cfg.xml

<bean id="oracleSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    <property name="hibernateProperties">
       <map>
            <entry key="hibernate.connection.provider_class" value="org.jasypt.hibernate.connectionprovider.EncryptedPasswordC3P0ConnectionProvider" />
            <entry key="hibernate.connection.encryptor_registered_name" value="configurationHibernateEncryptor" />      
            <entry key="hibernate.connection.driver_class" value="${jdbc.connection.driver_class}" />
            <entry key="hibernate.connection.url" value="${jdbc.connection.url}" />
            <entry key="hibernate.connection.username" value="${jdbc.connection.username}" />
            <entry key="hibernate.connection.password" value="${jdbc.connection.password}" />
            <entry key="hibernate.dialect" value="${jdbc.dialect}" />
            <entry key="hibernate.hbm2ddl.auto" value="${jdbc.hbm2ddl.auto}" />
            <entry key="hibernate.show_sql" value="${jdbc.show_sql}" />
            <entry key="hibernate.format_sql" value="${jdbc.format_sql}" />
            <entry key="hibernate.use_sql_comments" value="${jdbc.use_sql_comments}" />
            <entry key="hibernate.generate_statistics" value="${jdbc.generate_statistics}" />
            <entry key="hibernate.c3p0.min_size" value="5" />
            <entry key="hibernate.c3p0.max_size" value="100" />
            <entry key="hibernate.c3p0.timeout" value="30" />
            <entry key="hibernate.c3p0.max_idle_time" value="10"/>
            <entry key="hibernate.c3p0.max_statements" value="50" />
            <entry key="hibernate.c3p0.idle_test_period" value="30" />
        </map>
    </property>
</bean>

class

public void sendMessage(String msg) throws HibernateException, SQLException {

    Transaction transaction = null;
    Session session = null;     

    try {   

        session = this.getSession();        
        transaction = session.beginTransaction();

        HibernateDAO dao = new HibernateDAO(session);
        Message message = null;
        try {
            MessageJsonSerializer serializer = new MessageJsonSerializer();
            message = serializer.deserialize(msg);

            MessageSender sender = new MessageSender(dao, this.serviceConnectorFactory, this.jmsSender,
                    new MessageSenderListener(dao, this.jmsSender));
            sender.sendNowAssync(message);

        } catch (MessageBrokerException e) {
        e.printStackTrace(); 

        } finally {
            if (message != null) {
                PersistentMessage p = dao.findPersistentMessage(message.getId());

            }

        }

        transaction.commit();
        //session.disconnect();


    } finally {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        //session.close(); 
        //session.disconnect();

    }

}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.