Java Pool Hibernate c3p0

Dear, good afternoon.

I have a Java solution that is causing problems in production, bringing down the bank. After some 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 implemented method is flawed: there is a queue that manages sending messages, and with each new message a new session is opened for sending a single message.

My doubt: is it possible to establish a single session per user, to perform multiple transactions for sending the message? And when there is transabord for these transactions, open a new session per user? In the current configuration, 100 sessions are opened, which are not closed and stop sending 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();

    }

}
Author: Mariana Delphino, 2020-03-06