Does spring transaction management locks database? - spring

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.

Related

Why to use AOP for transaction management in Spring?

I am learning AOP and came to know that is useful for separation of concern, for example, logging, transaction management, security etc.
So far good to know AOP.
Now, I read about Spring transaction management in Spring framework we can have annotation #Transactional.
It is this point which is make me confused why should we use AOP in which we have to create Aspects rather than using the annotation which Spring provides.
For example:
#Transactional
public void dataAccessTrans() {
}
When Spring is already having transaction related functionality, then why should we use AOP to do transaction management?
If we use AOP, then don't we have to create Aspect and create advice which would act on the method; does't this make us to do manual work, rather than handling it by spring framework itself by its own annotations.
Can anyone help me understand this which I am not able to understand clearly.
Spring relies on AOP to implement declarative transactions.
The Spring Framework’s declarative transaction management is made
possible with Spring aspect-oriented programming (AOP), although, as
the transactional aspects code comes with the Spring Framework
distribution and may be used in a boilerplate fashion, AOP concepts do
not generally have to be understood to make effective use of this
code.
So as you use the #Transactional annotation :
#Transactional
public void dataAccessTrans() {
...
}
you use indirectly AOP.
So in the very most of cases, you never need to declare any custom aspects to handle the transaction management.

Spring boot with spring #Transactional works without enabling transactional management

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

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.

Is JPA a specification {Eclipselink and hibernate its implementation} how is it related to spring transaction management?

I am new to the ORM and till now whatever i read on the internet , it implies that JPA as just a specification and internally we need to use either eclipselink or hibernate or any other implementation .
But i got confuse when i saw Spring transaction management in one of my application. Is spring also a implementation of JPA
Can anybody explain what what is the actual difference between JPA ,
(EclipseLink.hibernate) , Spring.
JPA is an specification for object relational mapping, coming with a whole lot of interfaces and annotations that implementations must implement and support. One of the features provided by this specification is the ability to use local transactions with the JPA API.
Hibernate and EclipseLink are implementations of this specification.
Spring is a dependency injection framework that, among other things, allows you to handle transactions in a declarative way. But it doesn't come with any actual transaction manager. It just offers a common layer of abstraction over actual transaction management systems, one of them being JPA. But it can also use JDBC transactions, or JTA transactions. You just have to configure it to use the appropriate transaction management subsystem based on your requirements and technology choices.

Spring Framework Saving Data to Multiple Tables

Newbie here...
I'm working on a Spring MVC app and their JDBC api for data access. How would I go about saving data to multiple tables? The insertion needs to be such that if there's an error or something goes wrong, nothing gets inserted and rolled-back. Would this be Spring's transaction support? If so, the official documentation for transaction support is very confusing to me. Does anyone have a good source for learning how to do that?
I'm using Spring 3.1, Oracle 11g, and Tomcat 6.0
Yes, spring supports transaction. You can use DataSourceTransactionManager to configure your bean.IF you find xml mapping confusing you can use annotation #Transactional. Spring's annotation support for transactions are really simple. Spring in Action book has examples for transaction management.

Resources