3

I am trying to understand in real life Enterprise applications (e.g. web-application, using Spring Framework) who should manage the Data source? I can think of two ways:

(i) Data source defined in Spring configuration and on need basis the application can get the data source from the Spring container.

(ii) Data source defined in web-container (for example) and then the application can get the data source from the container.

Now, I have following questions:

Q1) In scenario (i) I believe we can use Spring's DI to inject the Data source as it is managed by Spring. Is it correct?

Q2) In scenario (ii), the only way to get the Data source would be using the JNDI lookup, as the data source is configured in the container, and hence Spring won't be able to do DI. Is the correct?

Q3) When the Data source is managed by the Spring Container, would it be able to handle connection pooling, global transaction etc., if so, how? Does it internally use some third party libraries to achieve all this?

Any detailed information on this really appreciated.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 1
    1. Yes, 2. No (what do you thing `` is for), 3. Yes, depends on what datasource you configure (see HikariCP for instance). – M. Deinum Jul 13 '16 at 08:01

1 Answers1

5

Basically:

  • Spring-defined data source makes you independent on the container, and it is easier to deploy your application
  • Container-defined data source gives the power and responsibility for the data source configuration in hands of the server admin instead of programmer
    • This follows arguably a better responsibility / role separation
    • It allows the server admin to maintain and update the data source without knowing details of your application
    • It solves a common "no DB passwords in plaintext" requirement, making the container responsible for securing the password instead of you

It is possible to refer to a JNDI resource in Spring configuration, so Spring will do the lookup for you and it will be available for DI.

You can use third-party libraries (e.g. Apache commons-dbcp) to do connection pooling for Spring-managed data sources (I've done so on enterprise projects).

Spring supports DB transaction management using the built-in TransactionManager. According to documentation, it seems JPA is only available if the data source is defined in container.

Community
  • 1
  • 1
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • 3
    I agree with you, but you can remove cleartext passwords from spring configuration (e.g, using jasypt) – raphaëλ Jul 13 '16 at 08:06
  • 1
    Sure, there are other ways to accomplish that, I don't write it's the only way to do so. It's quite convenient though, a good bonus to other reasons and personally it has been *the* reason to use JNDI for me couple times. – Jiri Tousek Jul 13 '16 at 08:10
  • @JiriTousek: Thanks for the detailed reply. So in situations where the Datasource is configured in Spring, how robust is the transaction management, connection pooling? Does Spring itself provide the support of there services or it internally uses third party libraries to do so? I am not clear in this point, can you please expand this point (#3 in my question) please? – CuriousMind Jul 13 '16 at 08:20
  • 1
    Edited - it seems JTA is only available for container-defined data sources; I don't have robust experience with JTA however so only quoting docs here. Transaction management is robust in Spring, third-party connection pooling libs are widely used. – Jiri Tousek Jul 13 '16 at 08:50
  • @JiriTousek Thanks so much for your reply, very informative. Thanks a lot! – CuriousMind Jul 13 '16 at 10:21
  • 2
    _"the JTA is tied to the container"_ No, it's not. There is variety of stand-alone transaction managers. – a better oliver Jul 13 '16 at 10:39
  • @zeroflagL thanks for clarification. Are any of those supported in Spring or are we stuck with container-provided JPA if we want to use JPA with Spring's transaction management? – Jiri Tousek Jul 13 '16 at 11:05
  • 2
    Atomikos and Bitronix (JTA providers) are easily integrated with Spring, and can easily be integrated with JTA. Refer to http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html for more details. – Miloš Milivojević Jul 13 '16 at 11:20