How to load the persistence.xml settings in ComboPooledDataSource?

0

I configured C3P0 via persistence.xml, however, when I try to retrieve the configuration values via ComboPooledDataSource, the received values differ from what was configured in the configuration file.

 <properties>    
            <!-- Configurações específicas do Hibernate -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

            <!-- Propriedades JDBC -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/financas" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="123" />

            <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/> 
            <property name="hibernate.c3p0.min_size" value="10" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.acquire_increment" value="1" />
            <property name="hibernate.c3p0.idle_test_period" value="3000" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.timeout" value="1800" />
            <property name="hibernate.c3p0.minPoolSize" value="10"/>
            <property name="hibernate.c3p0.maxPoolSize" value="20"/>
        </properties>

And in Java:

EntityManager em = JPAUtil.getEntityManager();

ComboPooledDataSource ds = new ComboPooledDataSource();
System.out.println("Numero de conexoes: " + ds.getMaxPoolSize());

I think the value returned by getMaxPoolSize () should be 20, but it is returning 15.

What now? how to load persistence.xml settings in ComboPooledDataSource?

----------------- UPDATED ---------

I added the properties:

    <property name="hibernate.c3p0.minPoolSize" value="10"/>
    <property name="hibernate.c3p0.maxPoolSize" value="20"/>

But the java continues to return maxPoolSize = 15 and now?

    
asked by anonymous 28.02.2017 / 21:34

2 answers

1

Correct properties:

Change this:

<property name="hibernate.c3p0.minPoolSize" value="10"/>
<property name="hibernate.c3p0.maxPoolSize" value="20"/>

To:

<property name="hibernate.c3p0.min_size" value="10"/>
<property name="hibernate.c3p0.max_size" value="20"/>

Why does not the snippet below work?

ComboPooledDataSource ds = new ComboPooledDataSource();
System.out.println("Numero de conexoes: " + ds.getMaxPoolSize());

Because what you are doing there is to create a new ComboPooledDataSource and retrieving the default value of it, 15. For you to retrieve the one defined in the Hibernate configuration you need to get the DataSource managed by Hibernate.

How to get the max_size of the DataSource used by Hibernate?

WrapperConnectionPoolDataSource wrapper = 
        (WrapperConnectionPoolDataSource) entityManagerFactory.unwrap(SessionFactoryImplementor.class)
                                                .getServiceRegistry()
                                                .getService(ConnectionProvider.class)
                                                .unwrap(PoolBackedDataSource.class)
                                                .getConnectionPoolDataSource();

System.out.println(wrapper.getMaxPoolSize());
System.out.println(wrapper.getMinPoolSize());

I do not know why you want to retrieve these values. This is somewhat insecure, since in order to do this you need to reference Hibernate's internal classes and methods and there is no guarantee that they will be present in future versions.

If you just want to know them to make sure that the values used in your persistence.xml correspond to those used by c3p0 , please be aware that during the hibernate startup logging in> console . It looks something like this:

INFORMAÇÕES: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@3f738772 [//as informações aparecem aqui dentro]
    
01.03.2017 / 01:44
1

The pool limit

<property name="hibernate.c3p0.minPoolSize" value="10"/>
<property name="hibernate.c3p0.maxPoolSize" value="20"/>
    
28.02.2017 / 22:13