samedi 18 avril 2015

@Transactional annotation is not working

This code is about some database actions:



@Component("trans")
public class CustomerTransactionImp extends JdbcDaoSupport{

public static final Logger logger = Logger.getLogger(CustomerTransactionImp.class.getName());

@Autowired
public CustomerTransactionImp(DataSource dataSource) {
setDataSource(dataSource);
}

@Transactional
public void deleteCustomerByName(String name){
String sql = "DELETE FROM CUSTOMER WHERE firstName = ?";

deleteCustomerByID((long) 100);
displayAllCustomer();
logger.info("Deleted customer named: "+ name);
getJdbcTemplate().update(sql,name);
}

public void deleteCustomerByID(Long ID) {
String sql = "DELETE FROM CUSTOMER WHERE id = ?";

logger.info("Deleted customer named: "+ ID);
getJdbcTemplate().update(sql, ID);
}

public List getAllCustomer(){
String sql = "Select * FROM customer";

return getJdbcTemplate().queryForList(sql);
}

public void displayAllCustomer() {
for (Object o : getAllCustomer()) {
System.out.println(o);
}
}
}


This is my Spring configuration file:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:context="http://ift.tt/GArMu7"
xmlns:jdbc="http://ift.tt/18IIlo0" xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG http://ift.tt/GArMu7 http://ift.tt/1jdLYo7 http://ift.tt/18IIlo0 http://ift.tt/1b0hqjk http://ift.tt/OGfeU2 http://ift.tt/18tm2Tg">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="database.transactionmanagement"/>

<jdbc:embedded-database id="dataSource">
<jdbc:script location="sqlfiles/schema.sql"/>
<jdbc:script location="sqlfiles/data.sql"/>
</jdbc:embedded-database>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
</beans>


And my test class:



@Test
public void trans_management_test() {
ApplicationContext context = new ClassPathXmlApplicationContext("databaseconf/transaction-conf.xml");

CustomerTransactionImp customerTransactionImp = context.getBean("trans", CustomerTransactionImp.class);

customerTransactionImp.deleteCustomerByName("Omer");
customerTransactionImp.displayAllCustomer();
}


So basic codes, in the code where I use the @Transactional annaotation, I created a wrong SQL query, the table is not my db-:



String sql = "DELETE FROM CUSTOMERWrong WHERE firstName = ?";


I mean, in the deleteCustomerByName method, here is deleteCustomerByID method and this method is working fine and commit.


According to transaction, deleteCustomerByID method must not commit or my understanding of the @Transactional annotation will be wrong. The SQL query under deleteCustomerByName method is wrong while the SQL query under deleteCustomerByID is okay, all of them must not commit???


Aucun commentaire:

Enregistrer un commentaire