在persistence.xml和spring配置文件中配置数据源之间的区别 [英] Difference between configuring data source in persistence.xml and in spring configuration files

查看:113
本文介绍了在persistence.xml和spring配置文件中配置数据源之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

1)配置内部持久性单元,如:

1) configuration inside persistence units, like:

<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)配置弹簧配置文件(如applicationContext.xml):

2) configuration inside spring configuration files (like 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?

推荐答案

如果你在JavaEE容器中,它会产生很大的不同。

除了个人偏好之外,如果你按照第二种方法进行一些修改,你会好得多。

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

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

In the first case, you're creating your own connection pool and 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.

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

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.

您最好的选择是通过JNDI查找容器的连接池即可。然后,您一定要尊重容器中的数据源配置。

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.

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



在部署到JavaEE容器时使用此



Use this when deploying to a JavaEE container

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




  • 请记住设置 JEE架构

  • 虽然Tomcat不是一个完整的JavaEE容器,但它确实通过JNDI管理数据源,所以这个答案仍然适用。

  • 这篇关于在persistence.xml和spring配置文件中配置数据源之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆