@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”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref= “sessionFactory” />
</bean>

JPA Transaction manager
<bean id=”txManager”
class=”org.springframework.orm.jpa.JpaTransactionManager”>
<property name=”entityManagerFactory” ref=”entityManagerFactory” />
</bean>
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
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(“com.jpaapps.spring.repository”)
@PropertySource(“classpath:database.properties”)
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”;

@Autowired
Environment environment;

@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lfb = new LocalContainerEntityManagerFactoryBean();
lfb.setDataSource(dataSource());
lfb.setPersistenceProviderClass(HibernatePersistence.class);
lfb.setPackagesToScan(“com.journaldev.spring.model”);
lfb.setJpaProperties(hibernateProps());
return lfb;
}

@Bean
DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUrl(environment.getProperty(PROPERTY_URL));
ds.setUsername(environment.getProperty(PROPERTY_USERNAME));
ds.setPassword(environment.getProperty(PROPERTY_PASSWORD));
ds.setDriverClassName(environment.getProperty(PROPERTY_DRIVER));
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;
}

@Bean
JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
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.

@Service
public class PersonService {

@Autowired
PersonRepository<Person> personRepository;

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

@Transactional
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