What’s a Transaction?
A database transaction is a set of operations which might be thought-about as in the event that they had been a single piece of labor. These actions ought to both end utterly or don’t have any impression. To guarantee knowledge integrity and consistency, transaction administration is an important side of RDBMS-based company functions.
The next 4 necessary qualities could also be used to characterize the thought of transactions:
- A transaction ought to be seen as a single unit of operation, which suggests the whole sequence of actions is both profitable or failed.
- Consistency refers back to the database’s referential integrity, in addition to the usage of distinctive main keys in tables.
- Isolation A number of transactions with the identical knowledge set could also be processed on the similar time. To keep away from knowledge corruption, every transaction ought to be separated from the others.
- Sturdiness Upon completion of a transaction, the outcomes should be made everlasting and can’t be faraway from the database as a result of a system failure.
- Use the start transaction command to start out the transaction.
- SQL queries could also be used to carry out quite a few delete, replace, and insert operations.
- If all the actions are profitable, commit them; in any other case, roll again all of them.
Each declarative and programmatic transaction administration is supported by Spring. Though EJBs require the usage of an utility server, Spring transaction administration could also be dealt with with out one.
@EnableTransactionManagement annotation in Spring
Just like the help offered in Spring’s <tx:*> XML namespace, this property permits Spring’s annotation-driven transaction administration performance. To arrange basic, crucial transaction administration or reactive transaction administration, make the most of @Configuration lessons.
1. The annotation-driven transaction administration performance is enabled by the @EnableTransactionManagement attribute. @Configuration lessons reap the benefits of it.
2. The @EnableTransactionManagement property configures both classical, crucial, or reactive transaction administration.
3. Within the configuration class, assemble a PlatformTransactionManager bean for crucial transaction administration.
4. Within the configuration class, assemble a ReactiveTransactionManager bean for reactive transaction administration.
5. The XML namespace <tx:annotation-driven/> is just like the @EnableTransactionManagement annotation.
That is all theoretical, let’s deep dive into code and perceive the way it works. Let’s create a spring challenge and a config class to deal with all of the configurations of our challenge.
Discover beneath the pattern configuration class. you possibly can add the imports on the go in your ide.
public class AppConfig {
public Cricketer cricketer() {
return new Cricketer();
}
public HibernateTemplate hibTemp() {
return new HibernateTemplate(sessionFactory(), true);
}
public SessionFactory sessionFactory() {
SessionFactory sf = new AnnotationConfiguration().configure()
.buildSessionFactory();
return sf;
}
public HibernateTransactionManager hibTransMan(){
return new HibernateTransactionManager(sessionFactory());
}
}
Now, let’s create a Cricketer object class for a similar.
public class Cricketer implements Serializable {
personal static closing lengthy serialVersionUID = 1L;
personal int id;
personal String identify;
public Cricketer(){
}
public Cricketer(int id,String identify){
this.id=id;
this.identify=identify;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return identify;
}
public void setName(String identify) {
this.identify = identify;
}
}
Now, let’s create a pattern class to check our code,
public class AppTest {
public static void fundamental(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
HibernateTemplate hibtemp= ctx.getBean(HibernateTemplate.class);
Cricketer c1= new Cricketer(1,“Virat”);
hibtemp.persist(c1);
Cricketer c2= new Cricketer(2,“Rohit”);
hibtemp.save(c2);
}
}
To cite the identical from spring docs, the beneath configuration file is similar because the XML file. Take a look.
config file :
@Configuration
@EnableTransactionManagement
public class AppConfig {
@Bean
public FooRepository fooRepository() {
return new JdbcFooRepository(dataSource());
}
@Bean
public DataSource dataSource() {
}
@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource());
}
}
XML file :
<beans>
<tx:annotation-driven/>
<bean id=“fooRepository” class=“com.foo.JdbcFooRepository”>
<constructor-arg ref=“dataSource”/>
</bean>
<bean id=“dataSource” class=“com.vendor.VendorDataSource”/>
<bean id=“transactionManager” class=“org.sfwk…DataSourceTransactionManager”>
<constructor-arg ref=“dataSource”/>
</bean>
</beans>
@EnableTransactionManagement is chargeable for registering the mandatory Spring elements that energy annotation-driven transaction administration, such because the TransactionInterceptor and the proxy- or AspectJ-based recommendation that weaves the interceptor into the decision stack when JdbcFooRepository’s @Transactional strategies are invoked, in each of the situations above.
Conclusion
On this article, we discovered about alternative ways of enabling the transaction together with the @EnableTransactionManagement annotation, how it’s used and the place and the best way to use it. We additionally noticed the equal XML configuration file. Now, you guys are all set and able to attempt it your self and use the facility of spring in your initiatives!
Thanks for studying this text thus far. If you happen to like these Spring
@EnableTransactionManagement annotation tutorials and examples then, please share them together with your
mates and colleagues.
In case you have any questions or suggestions, then please drop a be aware.