3

Recently I started a maven project to build an application for integrating

Spring, JPA, JSF

But in the automatically generated folder structure I can see one file named as

db.properties

and also I have one

persistence.xml

Now my question is that Database connection can be defined in either of these files, Can anybody tell me 1. Which way is better and why ? 2. Why there is db.properties file automatically generated while I already have persistence.xml ?

2 Answers2

0

I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.

When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.

Therefore, I recommend against configuring your datasource in persistence.xml.

See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.

Community
  • 1
  • 1
Kkkev
  • 4,716
  • 5
  • 27
  • 43
0

db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in

persistence.xml or dataSource.xml

whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.

 driverClassName=com.mysql.jdbc.Driver

this is an entry in your db.properties. and you will use it in persistence.xml as follows.

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}" />
ankit
  • 4,919
  • 7
  • 38
  • 63