How to load persistence.xml settings in Combopooleddatasource?

Asked

Viewed 174 times

0

I configured C3P0 via persistence.xml, however, when I try to recover the configuration values via Combopooleddatasource the received values are different 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 believe that the value returned by getMaxPoolSize() should be 20, but 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 java keeps returning maxPoolSize = 15 and now?

  • tries to add <property name="hibernate.c3p0.max_statements" value="20" />

  • You can also set directly by pool, ds.setMinPoolSize(10);&#xA;ds.setMaxPoolSize(20);

2 answers

1

failed to include within the persistent pool boundary

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

1


Correct properties:

Change that:

<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 the section below doesn’t work?

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

Because what you’re doing there is creating a new ComboPooledDataSource and recovering its default value, 15. To recover the one defined in the Hibernate configuration you need to obtain the DataSource managed by Hibernate.

How to get the Datasource max_size 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 don’t know why you want to get those values back. This is somewhat unsafe, since to do this you need to reference internal Hibernate classes and methods and there are no guarantees that they will be present in future versions.

If you just want to know them to ensure that the values used in your persistence.xml correspond to those used by c3p0, know that during the Hibernate startup the logging of that information on console. Something like this comes along:

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

Browser other questions tagged

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