@Transactional annotation in Spring

<tx:annotation-driven transaction-manager=”txManager”/>
Next is to define the transaction manager bean, with the same name as specified in the above transaction-manager attribute value.
The transaction managers could be –
DataSource Transaction manager
<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager><property name=”dataSource” ref= “datasource” /></bean>

Hibernate Transaction manager
<bean id=”txManager”
<property name=”sessionFactory” ref= “sessionFactory” />

JPA Transaction manager
<bean id=”txManager”
<property name=”entityManagerFactory” ref=”entityManagerFactory” />
We are now ready to use @Transactional annotation either at the class or method level.

Here’s the list of Propagation values with my comments:
MANDATORY-Support a current transaction, throw an exception if none exists.
NESTED-Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.
NEVER-Execute non-transactionally, throw an exception if a transaction exists.
NOT_SUPPORTED-Execute non-transactionally, suspend the current transaction if one exists.Does not start a transaction. Suspends any existing transaction.
REQUIRED-Support a current transaction, create a new one if none exists.
REQUIRES_NEW-Create a new transaction, suspend the current transaction if one exists.
SUPPORTS-Support a current transaction,execute non-transactionally if none exists.

@Transactional(value = “myTransactionManager”, propagation = Propagation.REQUIRED, readOnly = true)
public void myMethod() {


Spring Configuration Classes
public class DataConfig {
private final String PROPERTY_DRIVER = “driver”;
private final String PROPERTY_URL = “url”;
private final String PROPERTY_USERNAME = “user”;
private final String PROPERTY_PASSWORD = “password”;
private final String PROPERTY_SHOW_SQL = “hibernate.show_sql”;
private final String PROPERTY_DIALECT = “hibernate.dialect”;

Environment environment;

LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lfb = new LocalContainerEntityManagerFactoryBean();
return lfb;

DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
return ds;

Properties hibernateProps() {
Properties properties = new Properties();
properties.setProperty(PROPERTY_DIALECT, environment.getProperty(PROPERTY_DIALECT));
properties.setProperty(PROPERTY_SHOW_SQL, environment.getProperty(PROPERTY_SHOW_SQL));
return properties;

JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
return transactionManager;
@Configuration: this spring annotation says that it is configuration class.
@EnableTransactionManagement: this annotation allows users to use transaction management in application.
@EnableJpaRepositories(“com.journaldev.spring.repository”): indicates where the repositories classes are present.
@PropertySource(“classpath:database.properties”): says that we have property file in our classpath.

@Transactional annotation indicates that the method will be executed in the transaction. Spring will take care of transaction management.

public class PersonService {

PersonRepository<Person> personRepository;

public List<Person> getAllPersons() {
return (List<Person>) personRepository.findAll();

public List<Person> findByName(String name) {
return personRepository.findByFirstName(name);



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store