0

I am developing a web application based on Spring MVC framework. In this application I need to persist some data to DB.

I intend to use Spring data JPA as well. Now where is the best place to have the Datasource configured? I intend to deploy this in Apache tomcat.

I guess we have two places:

1) Define in the spring configuration file, like below:

<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.h2.Driver"/>
      <property name="url" value="jdbc:h2:tcp://localhost:9092/~/test"/>
      <property name="username" value="sa"/>
      <property name="password" value=""/>
 </bean>

OR

2) Define in the Tomcat.

Is my understanding correct? Are there any difference in approaches in #1 and #2?

If we use #2, can Spring do dependency injection to the Datasource when needed by the application? OR can we reference the Datasource by JNDI lookup in this scenario?

I am learning this of my own, to understand how real life applications work; so any deeper insight would be of great help.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
CuriousMind
  • 8,301
  • 22
  • 65
  • 134

1 Answers1

2

I have personally gone back and forwards between setting the datasource as JNDI and defining it in the configuration.

I came to the conclusion that I prefer defining the datasource in the configuration for a couple of reasons:

  • Using JNDI I would need to add the mysql-connector jar to the tomcat lib. And I don't like to have to change the running environment to cater for a specific app.
  • Doing the configuration in the web application, I can use annotation to configure, and no XML
  • I think is correct to say that is the application that needs to know how to connect to the database, not necessarily the container
Oliver
  • 1,360
  • 9
  • 14
  • Thanks for your reply. So it means to define the datasouce in the spring configuration file and then do the DI. If this is the case, will this approach be stable enough to handle multiple requests, connection pooling, transaction...? Aren't containers meant to tackle all these services? I am bit confused here, any other details if you can share to help clarify the doubt would be of great help. – CuriousMind Jun 15 '16 at 08:43
  • 1
    @CuriousMind if you don't use DriverManagerDataSource, but a connection pool datasource instead, then yes. See http://stackoverflow.com/questions/23172643/how-to-set-up-datasource-with-spring-for-hikaricp for example. What you're saying would be true if you used a container providing a JTA transaction manager that Spring would have to use. But Tomcat doesn't. All it provides is a connection pool. And you can embed a connection pool in the webapp instead. – JB Nizet Jun 15 '16 at 09:40
  • @JBNizet: Thanks for your comments. I am trying to understand how things work in real world application. I am still not clear on this. How can Datasource be stable, handle multiple transactions (local/global). If you can elaborate a bit, it would be of great help. I am searching on the net as well, however if I get some right guidance, it would save lot of time (and I have lost lot of time already..). Thanks for your time. – CuriousMind Jun 15 '16 at 16:01