Reputation: 19
We are working on a web application using Spring4 and Tomcat7.I am doing the database set up part first time so not sure about where to put the database confiuration part (datasource creation) in Spring context.xml or in tomcat context.xml What are the advantages of both these approaches and which is the better approach? Also,Our application is communicating with only one database.
Please help me out.
Upvotes: 0
Views: 933
Reputation: 16837
Presuming of course that this is a new application, I would recommend Spring Boot, with Spring Data JPA. Here is a link to the tutorial with the Spring Data Rest. Also I think that if you insist on manually wiring your Entity Manager and DataSource, you'd be best off doing it in JavaConfig. Spring Boot can configure the database for you then all you have to do is provide a connection string in application.properties
. Like so.
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Important do not commit your production database credentials to your source code. It's fine for localhost developer credentials. Spring Boot has facilities that will allow you to avoid this.
Although I do not recommend any XML approach as it is far more complicated than the Spring Boot alternatives and lacks type safety of Java Config.
You would choose Tomcat configuration if you were planning on running more than one application in that Tomcat instance that needed to use the same datasource. It could also be argued that an older reason for choosing this would be to have different credentials for different servers, or to keep credentials out of your source, however Modern Spring has several solutions built in for this.
You would choose a Spring configuration if you wanted it to be able to move to a different web server, such as Jetty or Undertow.
Upvotes: 2
Reputation: 380
If you are working on a college assignment or homework or experimental project, I think using H2 in-memory database is not a bad way to go.
Connecting to database is easy as adding a dependency to your pom.xml
<denpendencies>
//... other dependencies
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
//... other dependencies
</denpendencies>
Source / Tutorial: http://www.mkyong.com/spring/spring-embedded-database-examples/
Upvotes: 0