This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

What is the difference between configuring a data source in your persistence.xml and Spring configuration files?

I saw (and completed) the data source configuration in two places

  1. Configuration in the persistence layer
<persistence-unit name="LocalDB" transaction-type="RESOURCE_LOCAL">
    <class>domain.User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.c3p0.min_size" value="5"/ >... <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    </properties>
</persistence-unit>
Copy the code
  1. Configuration in spring configuration files such as ApplicationContext.xml:
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="JiraManager"/>
    <property name="dataSource" ref="domainDataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false"/>
            <property name="showSql" value="false"/>
            <property name="databasePlatform" value="${hibernate.dialect}"/>
        </bean>
    </property>
</bean>

<bean id="domainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driver}" />
    <property name="jdbcUrl" value="${datasource.url}" />
    <property name="user" value="${datasource.username}" />
    <property name="password" value="${datasource.password}" />
    <property name="initialPoolSize" value="5"/>
    <property name="minPoolSize" value="5"/ >... </bean>Copy the code

The question is: Are there pros and cons to each approach, or is it just a matter of habit?

Answer a

This makes a huge difference if you are in a JavaEE container.

Personal preference aside, it’s better if you follow the second method and modify it a little

In the first case, you are creating your own connection pool and will not benefit from the existing connection pool in the container. Therefore, even if you configure the container to have a maximum of 20 simultaneous connections to the database, you cannot guarantee this maximum because this new connection pool is not limited by your configuration. In addition, you will not benefit from any monitoring tools provided by the container.

In the second case, you also create your own connection pool, which has the same disadvantages as above. However, you can isolate the springbean definition and only use it in test runs.

The best way to do this is to look up the container’s connection pool through JNDI. Then you must respect the data source configuration in the container.

Used to run tests.

<! -- datasource-test.xml --> <bean id="domainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   <property name="driverClass" value="${db.driver}" />
   <property name="jdbcUrl" value="${datasource.url}" />
   <property name="user" value="${datasource.username}" />
   <property name="password" value="${datasource.password}" />
   <property name="initialPoolSize" value="5"/>
   <property name="minPoolSize" value="5"/ >... </bean>Copy the code

Use this option when deploying to a JavaEE container

<! -- datasource.xml --> <jee:jndi-lookup id="domainDataSource" jndi-lookup="jndi/MyDataSource" />
Copy the code

The article translated from Stack Overflow:stackoverflow.com/questions/3…