Spring boot with spring #Transactional works without enabling transactional management - spring

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

Related

What's the difference between Spring Boot's #Configuration and #AutoConfiguration?

And most important, what's the reason behind the "Auto" prefix? Classes annotated with #Configuration rather than #AutoConfiguration are less automatic or something?
#Configuration is a spring framework annotation and not strictly bound to spring-boot. It was introduced when spring started to allow programmatic creation of spring-beans as to move forward from xml definitions of beans.
#AutoConfiguration is a spring-boot specific annotation not commonly available in spring framework. The reason it exists, is for external providers that cooperate with spring-boot to be able to mark some classes in some libraries they provide with this annotation as to inform spring-boot that those classes could be parsed and make some initializations during start up of spring application automatically.
So if some regular programmer that develops some application happens to have kafka in dependencies then some beans will automatically be created and added in application context and will be ready for the programmer to use, although he has not defined any configuration for them. Spring-boot already knows this as the kafka provider has already informed by marking some class in the jar they provide with the annotation #AutoConfiguration.
For this reason #AutoConfiguration has some more powerful configurations available as before, after, beforeName, afterName as to allow the provider to specify when the configuration is applied during application startup if some order is necessary.
So this annotation is not to be used from some regular programmer that develops an application using spring-boot. It is for someone that develops a library that other users might use with spring-boot. One such example is kafka library.
For this to work in a spring-boot project #EnableAutoConfiguration is needed as well, to enable auto configuration.
From spring documentation
Spring Boot auto-configuration attempts to automatically configure
your Spring application based on the jar dependencies that you have
added. For example, if HSQLDB is on your classpath, and you have not
manually configured any database connection beans, then Spring Boot
auto-configures an in-memory database.
#Configuration instead is to be used from some regular programmer that develops an application using spring-boot or spring-framework as to inform the framework for which beans should be created and how.
#AutoConfiguration was introduced in 2.7 with the idea to mark all auto-configurations with its dedicated annotation and move away from spring.factories for auto-configuration imports in 3.0 as described in Github issue.
According to Spring documentation:
[#AutoConfiguration] indicates that a class provides configuration that can be
automatically applied by Spring Boot. Auto-configuration classes are
regular #Configuration with the exception that
Configuration#proxyBeanMethods() proxyBeanMethods is always false.
Usually, #AutoConfiguration classes automatically configure an application based on the dependencies that are present on the classpath. Those classes are generally marked as #ConditionalOnClass and #ConditionalOnMissingBean annotations that detect the presence or absence of specific classes.
Additionally, if a configuration needs to be applied in a specific order, you can use the before, beforeName, after, and afterName attributes on the #AutoConfiguration, unlike #Configuration which doesn't provide those attributes.

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 JPA without Spring data

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.

Spring Boot says that JPA doesn't support custom isolation levels, but it seems like spring can do it

In spring boot's BasicBatchConfigurer there is a warning presented:
logger.warn("JPA does not support custom isolation levels, so locks may not be taken when launching Jobs");
However, I get the impression that this issue has been addressed: https://jira.spring.io/browse/SPR-11942
Is there a work around for this issue? Should I just be extending BatchConfigurer and implementing my own createJobRepository method? Should there be a change to spring boot code?

Spring Hibernate Connection through AOP standalone application

I am trying to develop Annotation based Spring Hibernate standalone application to connect to DB. I've gone through the some blogs and wondered like we should not make use of hibernateTemplate becoz coupling your application tightly to the spring framework. For this reason, Spring recommends that HibernateTemplate no longer be used.Further more my requirement is changed to Spring Hibernate with AOP using Declarative Transaction management.I am new to AOP concepts. Can any one please give an example on Spring Hibernate Connection through AOP. That would be a great help to me.
Thanks in advance.
If you are looking for exemples of project structures, you may want to use maven archetypes which provide you an already working Spring + Hibernate or Spring + JPA configuration.
They may provide you also a web layer (or not) but you can remove it if you want.
To try that, install maven and type:
mvn archetype:generate
By the way, I don't think using HibernateTemplate is a big deal. Many people still use it. But you'd better inject the Hibernate session factory and use contextual sessions with getCurrentSession()
I'd use JPA instead of plain Hibernate. You can of course use Hibernate as a provider. I guess that you know how to run Spring container in standalone application. Just follow the steps from documentation here. Use LocalContainerEntityManagerFactoryBean. Then read about transaction management.
There is a new feature that lets you start JPA without persistence.xml file. Read here.
If you still want to use plain Hibernate follow the docs.

Resources