Java 在persistence.xml和spring配置文件中配置数据源的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3111992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:32:26  来源:igfitidea点击:

Difference between configuring data source in persistence.xml and in spring configuration files

javaspringjpapersistence

提问by Roman

I've seen (and done) data source configuration in two ways (the code below is just for demo):

我已经通过两种方式看到(并完成了)数据源配置(以下代码仅用于演示):

1) configuration inside persistence units, like:

1) 持久化单元内的配置,如:

<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>

2) configuration inside spring configuration files (like applicationContext.xml):

2)spring配置文件里面的配置(比如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>

The question is: are there any pros and cons of each way, or it's just a matter of taste?

问题是:每种方式都有什么优点和缺点,还是只是品味问题?

采纳答案by Leonel

It makes a huge difference if you're in a JavaEE container.

如果您在 JavaEE 容器中,这会产生巨大的差异。

More than personal preference, you're much better off if you follow the second approach with a few modifications.

不仅仅是个人喜好,如果您遵循第二种方法并进行一些修改,您的情况会好得多。

In the first case, you're creating your own connection pooland do not profit from the existing connection pool in the container. Thus even if you configured your container to, say, a max of 20 simultaneous connections to the database, you can't guarantee this max as this new connection pool is not restrained by your configuration. Also, you don't profit from any monitoring tools your container provides you.

在第一种情况下,您正在创建自己的连接池,并且不会从容器中现有的连接池中获利。因此,即使您将容器配置为最多 20 个同时连接到数据库,您也不能保证这个最大值,因为这个新连接池不受您的配置限制。此外,您不会从容器为您提供的任何监控工具中获利

In the second case, you're also creating your own connection pool, with the same disadvantages as above. However, you can isolate the definition of this spring bean and only use it in test runs.

在第二种情况下,您还要创建自己的连接池,但具有与上述相同的缺点。但是,您可以隔离此 spring bean 的定义,并仅在测试运行中使用它。

Your best bet is to look up the container's connection pool via JNDI. Then you are sure to respect the data source configurations from the container.

最好的办法是通过 JNDI 查找容器的连接池。那么你一定要尊重来自容器的数据源配置。

Use this for running 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>

Use this when deploying to a JavaEE container

在部署到 JavaEE 容器时使用它

<!-- datasource.xml -->
<jee:jndi-lookup id="domainDataSource" jndi-lookup="jndi/MyDataSource" />
  • Remember to set the JEE schema
  • Although Tomcat is not a full JavaEE container, it does manage data sources via JNDI, so this answer still applies.
  • 记得设置JEE 架构
  • 虽然 Tomcat 不是一个完整的 JavaEE 容器,但它确实通过 JNDI 管理数据源,所以这个答案仍然适用。

回答by Jesse Webb

It is strictly personal preference.

这是严格的个人喜好。

My suggestion would be to use Spring's configuration if you are using Spring already. Its purpose is dependency injection and management so let it do its job with respect to your dependency on a database. If, however, you are not already using Spring, stick with the persistence configuration considering that this will keep your project simpler while still functional. I will suggest though that any project that needs Hibernate to interact with a database is probably big enough to condone using Spring within.

如果您已经在使用 Spring,我的建议是使用 Spring 的配置。它的目的是依赖注入和管理,所以让它在你对数据库的依赖方面完成它的工作。但是,如果您还没有使用 Spring,请坚持使用持久性配置,因为这将使您的项目在保持功能的同时更简单。不过我会建议,任何需要 Hibernate 与数据库交互的项目都可能大到足以容忍在其中使用 Spring。