0% found this document useful (0 votes)
10 views5 pages

Video Script ORM Latest 11 9

The document outlines the configuration of transaction management in an employee management application using Spring. It details the process of removing boilerplate code from the addEmployee method by utilizing the @Transactional annotation and configuring a custom JpaTransactionManager in the XML configuration. The document also explains how to manage transactions automatically and test the functionality through a UI tester class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Video Script ORM Latest 11 9

The document outlines the configuration of transaction management in an employee management application using Spring. It details the process of removing boilerplate code from the addEmployee method by utilizing the @Transactional annotation and configuring a custom JpaTransactionManager in the XML configuration. The document also explains how to manage transactions automatically and test the functionality through a UI tester class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Video script Transaction management

We will see configuration of transaction management in employee management


application. We have imported this application in eclipse. As we can see here, we
have already written all the classes related to this application in various packages.
in DAO package we have DAO interface. in this interface we have 3 methods to
perform curd operations. With DAO implementation class we have implemented
DAO interface and we have given implementation of all those methods.
In the implementation class, we have injected entity manager factory. Using the
entity manager factory we have created entity manager in every method. Apart
from this, in each method we created the transaction and managed the
transaction with the help of begin and commit methods.
We want to remove this boilerplate code. Initially we are going to remove
boilerplate code from the addEmployee method .
For doing this instead of injecting entity manger factory we will inject entity
manager with the help of persistence context annotation . So, I am adding
Persistence context annotation with entity manager, and I am commenting on
previous lines of code where we had created entity manger factory.

We can comment the lines of code declaring EntityManager in the addEmployee


method
As EntityManager is going to be provided by Spring, so managing the transactions
of the entity manager will also be Spring’s responsibility.

To let Spring manage transactions, we need to use @Transcational annotation.


Let me add @Transactional annotation on top of addEmployee method.

So, now this addEmployee method is a transactional method. Whenever it is


called, Spring will automatically begin transaction before this method is called and
will end Transaction after the method has been executed. So we need not call
begin and commit method manually. So, we can comment begin and commit
method on the add employee method. We can also comment the lines of code to
close the EntityManager.

For Spring to automatically manage transactions it makes use of a transaction


Manager.
We need to configure transaction Manager in XML configuration.
Let us open spring-jpa-config.xml file which is in resources package. All Spring
ORM related beans are configured in this file.
And we see that this file is imported in spring-main-config XML file.
In spring-jpa-config xml file we see that DriverManagerDataSource bean is
instantiated to create data source instance. We make use of data source bean to
instantiate LocalContainerEntityManagerFactoryBean.

To configure the Transaction manager, we need to instantiate


JpaTransactionManager bean. Let us create a bean of type
JpaTransactionManager using bean tag. Default name of the TransactionManager
is transactionManager but customize it, in id attribute we can assign the name of
the custom transaction manager as txManager.
Within bean tag we set the property tag with name attribute and assign
reference of entity manager factory which is entityManagerFactory to it.
EntityManagerFactory bean refers to the DataSource to obtain the database
connection information. So, the JpaTransactionManager is referring to the
EntityManagerFactory bean to manage the transaction for the database
connections.
Now we have created a custom transaction manager.
To enable custom transaction manager, we need to mention it in tx:annotation-
driven element with the transaction-manager attribute which is assigned with
custom transaction manager name i.e txManager.
<tx:annotation-driven transaction-manager="txManager"/>

Now we have done all the required configurations of Transaction management.


We have created the custom Transaction Manager, so we need to refer the same
in the DAO class.
Goto the DAO class, in the @Transactional annotation will add the value attribute
and assign the value as the custom transaction manager name i.e txManager.

For second video===


To execute this application, we need to create a database with the help of
database script which is already written in resources folder.
Let us open the HeidiSQL, enter the password and database schema which we
created, click on open. We run the database script.
Expand the database schema, look at the table available.
Let us see the table created and records available.

Let us now test addEmployee method.


We see that addEmployee method of DAO is called from service class and
addEmployee method of service is called from UI tester class. It has a main
method. Let us run it.
We can see in console that DAO method ran successfully, and Hibernate was able
to fire INSERT query.
Let us also check in DB if new record has been created.
We see that record exists in employee table.

3th video==
Now the boilerplate code in the addEmployee is removed. We can do the same in
other DAO methods as well.
So, Let us go to DAO class. We can place the @Transactional annotation in the
class level as well. So that all the methods transaction will be managed implicitly.
It means before the method execution starts transaction starts, after successful
execution of the method, transaction gets committed.
Now we can remove the code snippets of creating the entity manager, creating
transaction, managing the transaction using begin and commit methods.

4th video.
I am adding Transactional annotation at class level in DAO implementation class.
In the value attribute of this annotation, we will assign custom transaction
manager name which is txManager .we will create custom transaction manager
Later in jpa spring configuration file.
So, whenever any curd method is going to execute automatically, the transaction
begins, and once method executed transaction will commit automatically. Now
we can comment or remove begin and commit method from add employee
method.
Now we can see we have removed a lot of boilerplate code from the add
employee method.
Now we need to configure springjpa.xml file which is written in resources
package. in this file we must instantiate jpa transaction manager bean
And create a custom transaction manager TX manager. for doing so we need to
inject entity manager factory in jpa transaction manager bean. Now we open a

<bean id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManage
r"> <property name="entityManagerFactory
ref="entityManagerFactory" />
</bean>
bean tag and in id attribute we assign the name of custom transaction manager
which is TX manager. in the class attribute we will assign JpaTranscatonManager.
Within bean tag we will open a property tag. in the property tag name attribute,
we assign entity manager factory and in the reference attribute we assign
reference of entity manager factory which is entityManagerFactory
Now we have created a custom transaction manager. Now we open annotation.
<tx:annotation-driven transaction-manager="txManager"/>

driven element, it has attribute transaction manager in this we assign TX manager


which is custom transaction manager name. Now we have done all the required
configurations. for executing this application, we will create required database.
database script is already written in resources folder. We will open it and run the
database script. Now we can check the employee table and we can see we have
three employee records currently.
Now we will go to Ui tester class and enable the add Employee method and run
the Ui tester class. Now we can check the result on the console screen. We can
see here select Query has been executed. We can check database also run the
select query for the employee table and we can see newly inserted record and we
have four records currently.

You might also like