在持久性.xml和spring配置文件中配置数据源之间的区别
2022-09-01 05:24:58
我已经通过两种方式看到(并完成了)数据源配置(下面的代码仅用于演示):
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)弹簧配置文件内部的配置(如应用程序上下文.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>
问题是:每种方式是否有任何优点和缺点,或者这只是一个品味问题?