Difference between a Spring datasource and a Tomcat datasource? - spring

What is the difference between a Spring datasource and Tomcat datasource? Any pro's / con's? Is there a favored choice?

When using a Tomcat datasource you have to drop the JDBC driver JAR file(s) in Tomcat's classpath (the Tomcat/lib). This is sometimes not affordable/possible, for example when it concerns 3rd party hosting with zero server admin rights. When using a Spring managed datasource, it's sufficient to just drop the JDBC driver JAR file(s) in webapp's classpath (the Webapp/WEB-INF/lib). Plus, I'd imagine that you've in Spring the additional benefit that you don't need to grab the DataSource manually. Also, you have the freedom of choosing a specific connection pool. Also, Tomcat ships with DBCP builtin as default connection pool which is not the best choice per se. With Spring, you could for example choose BoneCP above DBCP without the need to fiddle with Tomcat default configuration/classpath.

If you have a datasource in the Spring config, you associate it with a Tomcat datasource when you are in a web application. You can associate it with something else, for example Spring DriverManagerDataSource when you run unit tests.

Related

Get NoInitialContext Spring Boot 2.0.x Embedded Tomcat Resource and DataSource Configuration Using JavaConfig

Following other links I have tried all configurations including enabling jndi of embedded tomcat container. (A very good detailed like is: https://www.roytuts.com/spring-boot-jndi-datasource/) But the problem is that the DataSource is looked up against JNDI and I get follwoing exception:
Please note that From Spring 2.0.x Embedded configuration classes have changed.
Get:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial.
As for the question of JNDI lookup in Embedded tomcat is rare use case I am trying to simulate and reproduce a Database Connection Pooling Error in production and for that I have to use JavaConfig settings for Resource and DataSource both.

quarkus with application managed persistence

Actually, I use WildFly JEE Server and consider to switch to Quarkus. I have the following questions about quarkus:
1. persistence.xml
I see that quarkus uses its own application.properties to set up database. Can I use persistence.xml instead?
2. container managed persistence
Does quarkus provide something similar or must I manage persistence by myself?
Regarding your question around Container Manager Persistence;
You can mark any bean method with the standard #Transactional annotation.
The Transaction Manager is automatically setup and configured with reasonable defaults; see the Transactions Guide to reconfigure.
The Datasource (connection pool) is integrated with the Transaction Manager, and optionally allows for XA. See Datasource Guide
Hibernate ORM is integrated with all of the above automatically - it's effectively running in "JTA Mode"
You can use CDI's standard #Inject to get an EntityManager
or you can use Hibernate ORM with Panache to not even need an EntityManager :-) It will still bind to the transactional components.
If you don't like how this is integrated for you, or just prefer old-style configuration, you can use the configuration via persistence.xml as an alternative.
persistence.xml is supported, see this

Spring boot datasource specific properties for embedded jetty server

I have spring boot application and it's basically a gradle project, so, I have below dependency added in my gradle file:
org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE
Application gets deployed in embedded jetty server. I have following set of properties in application.properties for db connection polling:
spring.datasource.driver-class-name
spring.datasource.max-active
spring.datasource.max-idle
spring.datasource.min-idle
spring.datasource.validation-query
spring.datasource.name
I was referring to below two links:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Configuration-Changelog
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.5-Configuration-Changelog
I came to know that some of the datasource properties which used in application.properties file in my application are removed starting sprint boot version 1.4 but issue they haven't mentioned what are the new properties to use. Like for tomcat server they have provided all set of properties but not for jetty server. I am facing some db related errors like 'too many connections' after my application run for sometime, my assumption is that datasource props I am currently using are not correct and should be replaced with correct values, but unfortunately I am unable to find correct property names.
Jetty doesn't have its own Pooling DataSource implementation. You can include HikariCP in your project and customise using the spring.datasource.hikari.* properties. With each property matching the bean properties that can be set on Hikari's datasource implementation.

Alternative of JTATransaction

I have an Spring based J2EE application which runs well on Weblogic, I wanted to move it to Tomcat.
It seems tomcat doesn't support JTA Transaction Manager without external jar help like Atomikos, JOTM, Bitronix, SimpleJTA.
I am reluctant to make changes into my application where i am already using annotation based JTA transaction manager.
Are there alternatives for JTA Transaction Manager which I can use so that I am able to switch from weblogic to tomcat or tomcat to weblogic or any other server without changing my configuration file each time?
All in all what's best for transaction manager configuration when you want to keep your application (war) independent of server(s).
You could try TomEE.
It's a Java EE 6 server that meets the Web Profile requirements and is based on Tomcat.
So it will support JTA transactions.
You can get it from http://tomitribe.com
Just to give you a more direct link to TomEE: http://tomee.apache.org/download/tomee-1.7.2.html
If your application is configured and developed to use Weblogic then chances are you are using JDNDI to lookup the JTA transaction manager and your datasources.
So any solution that supports the same lookups would work.
For Atomikos, we recently added (commercial) support for Tomcat's JNDI space - check out http://www.atomikos.com/Main/BuyOnline to learn more.
Hope this helps!

JBoss 7 Spring Oracle connection pooling

I want to use Spring framework to create application that uses Oracle 11g to store data.
Can you tell me how to configure the Spring framework to use the connection pooling with JBoss 7 and Oracle 11g.
Is it possible to put this code onto OSGI bundle?
You have to configure your datascource in JBoss first. After configuring your datasource it should hava a JNDI name like "jdbc/yourDataSource".
You can now reference this datasource from your spring context.
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/yourDataSource"/>
Have a look at the documentation
If you use relative JNDI names then you have to put additional settings into the web.xml. For global nameing set resource-ref="false". For Spring it should not matter what database you use as its managed by the application server.
I cannot tell you if you can use it in a OSGI bundle.

Resources