I am working on a requirement for multi-tenant application which should support mulitple datasources. I have used AbstractRoutingDataSource for the JPA repositories, now I need to extend it to spring jdbc template for a new application. Is there any reference/example that can be used for reference?
Take a look at this article. baeldung
Having configured the AbstractRoutingDataSource with the actual data source instances, it's a simple case of then wiring the AbstractRoutingDataSource into the JdbcTemplate instance and also into the TransactionManager. JdbcTemplate is thread safe and when you perform an operation against the single JdbcTemplate instance, the datasource that is used will be based on the datasource that was set on the context holder for the executing thread.
Related
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
I am new in spring transaction management. I have learn in hibernate there is locking concept for transaction management.
Does spring have such internal working Mechanism.
Yes there are locking concepts implemented in Spring. So if you are using Spring Data JPA you can simply use the #Lock annotation to decide on how you want to lock within your transaction. You can find further information here. In this example the Annotation is only used on the Repository interface methods, but as far as I know you can also use this on Service methods annotated with #Transactional.
In my spring boot application spring #Transactional annotation works without specifying #EnableTransactionManagement explicitly.
Is there any official documentation saying that it is enabled automatically?
Or there is something else happening .... ?
btw: I'm using Spring Data JPA
Yes, this is enabled as long as you have spring-tx and some transactional resource in your application. Effectively if you are using spring-boot-starter-jdbc or spring-boot-starter-data-jpa, Spring Boot will configure a DataSource for you, start Hibernate (in the latter case) and configure transaction management.
Not all "Enable" annotations require to be explicitly set. When there is a reasonable amount of things that we can check to validate it makes sense to configure that for you, we'll do it. In this case, if you have a DataSource you must probably want to have transactions. If you have JPA (and no JTA infrastructure), you probably want a JpaTransactionManager). If we auto-configure that, the easiest way to use it is via #Transactional so we'll enable that in that case as well.
I guess you kept asking to get some sort of "official" answer, so here's one.
#SpringBootApplication adds #EnableAutoConfiguration it detects Spring Data JPA on your classpath. According to it Spring registers PlatformTransactionManager - JpaTransactionManager, datasource, entitymanager, repositories.
Not sure there are precise articles, but there are proper answer on stack. An official spring sample article
#Transactional annotation can work fine if the "< tx:annotation-driven/ >" tag is in your Spring XML configuration. Look at this reference : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html
I'm gradually introducing Spring Boot to a Spring JPA project. My intent was to first introduce Spring Boot, than at some later stage Spring Data, but I was not able to find any examples (nor a suitable starter) that uses Spring Boot + JPA without Spring Data.
How come? Is there any benefit of introducing Spring Boot to Spring JPA project, without Spring Data, or does it make sense only with Spring Data in place.
Any article link or example code would be helpfull and appreciated, thanks
More context
I'm working with a live project so every change introduces risk. We're discussing of moving from XML to JAVA based configuration, and I'm advocating adopting Spring Boot at a same time, but I lack persuasive selling points.
Personally, I want to include Spring Boot on all layers to boost future productivity, but I need to argue better the direct immediate benefits of using it in our Service/DAO module which is at the moment based on Spring/JPA/Hibernate with the good old manual CRUD implementations.
So I need selling points for using Spring Boot on a persistence layer, but ones that span beyond Spring Data (e.g. configuration gains, maintenance, testing...anything)
As folks have said above, there is no Spring Boot JPA. It's either Spring Boot Data JPA, or JPA on its own.
The immediate benefits that I could think of:
With Spring Data JPA you don't write the Dao layer. For all CRUD operations, the CrudRepository interface gives you all you need. When that is not enough, all you have to use is the #Query annotation to fine-tune your SQLs
Configuration by convention. For example, with Spring Boot, just having the H2 dependency in the classpath gets Spring to use the H2 in-memory database, gives you Datasource configuration and transaction management (only at the JPA repository level) by default
Ability to create micro-services. With Spring Boot, you can create micro services that can be deployed and run on a number of boxes with java -jar ...
You can enable annotation-based transaction with one simple annotation: #EnableTransactionManagement
Java configuration over XML. This advantage is not to be underestimated
A lot less code (the DAO layer) means also a lot less maintenance
The native ability to provide a RESTful API around data: https://spring.io/guides/gs/accessing-data-rest/
It all depends where your company is heading for. If they want to deliver business value faster and move towards more a DevOps operating model, then the above advantages should be enough selling points for any organisation
Spring wiht JPA (for example Hibernate) but without Spring-Data-Jpa means that you direct interact with the JPA Entity manager and. Typical you use it to implement your own DAO from it and use the #Respository annotation.
#Respository
public class UserDao {
#PersistenceContext EntityManager em;
public User findUserByLogin(Sting login) {
....
}
}
Even if there is no starter project, you could use a Spring-Data-JPA project, and implement the Repository in this old fashion style. (And then you could show how simple it become when you just write Spring-Data-JPA interfaces)
As far as I known, spring-boot means more convenient not any independent business feature.
In other words, spring-boot helps you to start, configure your application in some automatically way. But you can do that without spring-boot with your own specific configuration.
So, you are going to use spring-boot in your application means you are going to use spring-boot's auto configuration feature with your original application.
Actually, Spring JPA implemented in spring-data-jpa is what you are looking for not spring-boot. Of course, spring-boot can simplify your work dramatically.
Is there a way to configure hibernate type overriding using Spring and Spring Data JPA?
Using hibernate I did following:
configuration.registerTypeOverride(new CustomType);
Since I'm using Spring LocalContainerEntityManagerFactoryBean I'm not aware if I can do it or not. Is it possible?