I am implementing Spring Transaction in my application service layer..
I was referring to example on tutorials point for programmatic way of implementing spring transaction...
https://www.tutorialspoint.com/spring/programmatic_management.htm
I followed each & every step mentioned over there....but getting issue with bean creation in the xml file..
Error:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [database/spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [org.springframework.jdbc.datasource.DataSourceTransactionManager]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
system & application configuration:
OS: ubuntu 16.0.4
spring version: - 5.0.3
tomcat: 9
jdk: 1.9
IDE: Eclipse Oxygen 3
It will be very helpful for me if any one can give me the solution...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- ********************* Initialization for Inventory database -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myDatabase" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!--********************* Initialization for TransactionManager -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
public class MyService implements InventoryServiceIface{
private MyDAO dao;
private PlatformTransactionManager transactionManager;
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void setDAO(MyDAO dao) {
this.dao = dao;
}
public Student saveStudent(Student tudent) throws ServiceException{
validate(Student); // validate the data inside party object
TransactionStatus status = beginTransaction(); // begin database transaction
try
{
rollbackTransaction();
Student = dao.saveStudent(student);
}
catch(ServiceException e)
{
rollbackTransaction(status);
throw e;
}
catch(Exception e)
{
rollbackTransaction(status);
throw new ServiceException(e);
}
commitTransaction(status);
return student;
}
public TransactionStatus beginTransaction()
{
System.out.println("TRANSACTION BEGINS....");
return transactionManager.getTransaction(new DefaultTransactionDefinition());
}
public void rollbackTransaction(TransactionStatus status)
{
System.out.println("ROLL BACK....");
transactionManager.rollback(status);
}
public void commitTransaction(TransactionStatus status)
{
System.out.println("TRANSACTION COMMITTED....");
transactionManager.commit(status);
}
}// End of Class
The property name of DataSourceTransactionManager that set the datasource is as writen in the stacktrace dataSource and not xxxxxDataSource . you config should be as follow
<bean class="org.springframework.jdbc.datasource.
DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="xxxxDataSource">
</property>
</bean>