Hi all, I want to test the transactionality of my service. Following is my configuration file:
my serice is:HTML Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:property-placeholder location="/WEB-INF/spring/hibernate-config/spring.properties" /> <!-- Enable annotation style of managing transactions --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html --> <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="${hibernate.config}" p:packagesToScan="it.obiectivo.ecoss"/> <!-- Declare a datasource that has pooling capabilities--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${app.jdbc.driverClassName}" p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}" p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="100" p:maxStatements="50" p:minPoolSize="10" /> <!-- Declare a transaction manager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> </beans>
HTML Code:@Service("accessService") @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public class AccessService { protected static Logger logger = Logger.getLogger("gestoreService"); @Autowired private GestoreDAO gestoreDAO; @Autowired private UtenteDAO utenteDAO; @Autowired private DistributoreDAO distributoreDAO; @Autowired private CredenzialiutenteDAO credenzialiutenteDAO; @Autowired private SessionFactory sessionFactory; /* ------------------------------------------ * * METODI PER L'Aggiunta DEGLI Accessi * * ------------------------------------------ */ //Aggiungo un nuovo Accesso (Gestore + Credenzialiutente) /*PARAMETRI DI INPUT: Entity, String, String*/ /** * @todo * Il metodo attualmente è reso ATOMICO attraverso l'uso di istruzioni * esplicite come: begin(), commit() e rollback(). Lo stesso dovrebbe * integrare le stesse funzionalità in automatico grazie all'utilizzo * dell'annotazione: @Transactional o delle sue varianti * es. @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = true) */ public Boolean add(Gestore gestore, Credenzialiutente credenziali) throws Exception { logger.debug("MakeAccessService: - creazione di un nuovo accesso x Gestore"); Date dataInserimento = new Date(); gestore.setGestDataCreazione(dataInserimento); credenziali.setGestore(gestore); credenziali.setDataInserimento(dataInserimento); credenzialiutenteDAO.add(credenziali); return true; } //Aggiungo un nuovo Accesso (Distributore + Credenzialiutente) public Boolean add(Distributore distributore, Credenzialiutente credenziali) { logger.debug("MakeAccessService: - creazione di un nuovo accesso x Distributore"); Gestore gestore = gestoreDAO.getByRagSociale(distributore.getGestore().getGestRagSociale()); distributore.setGestore(gestore); Date dataInserimento = new Date(); distributore.setDistDataCreazione(dataInserimento); credenziali.setDistributore(distributore); credenziali.setDataInserimento(dataInserimento); credenzialiutenteDAO.add(credenziali); return true; } /** @todo * Dovrei trovare un sistema per poter ritornare, in caso di errore, il valore booleano. * In particolare almeno per adesso, dalle ricerche effettuate, sembrerebbe che per poter * far funzionare correttamente il meccanismo delle transizioni, non si può usare il * try\catch statemen. * **/ //Aggiungo un nuovo Accesso (Utente + Credenzialiutente) public Boolean add(Utente utente, Credenzialiutente credenziali) { logger.debug("MakeAccessService: - creazione di un nuovo accesso x Utente"); Gestore gestore = gestoreDAO.getByRagSociale(utente.getGestore().getGestRagSociale()); utente.setGestore(gestore); String distRagSociale = utente.getDistributore().getDistRagSociale(); if(!distRagSociale.equals(null)){ Distributore distributore = distributoreDAO.getByRagSociale(utente.getDistributore().getDistRagSociale()); utente.setDistributore(distributore); } Date dataInserimento = new Date(); utente.setUteDataCreazione(dataInserimento); credenziali.setUtente(utente); credenziali.setDataInserimento(dataInserimento); credenzialiutenteDAO.add(credenziali); return true; } /* ------------------------------------------ * * METODI PER L'Eliminazione DEGLI Accessi * * ------------------------------------------ */ //Elimina un accesso x Gestore public Boolean delete(Gestore gestore) { logger.debug("MakeAccessService: - eliminazione di un accesso x Gestore"); gestoreDAO.delete(gestore.getIdGestore()); return true; } //Elimina un accesso x Distributore public Boolean delete(Distributore distributore) { logger.debug("MakeAccessService: - eliminazione di un accesso x Distributore"); distributoreDAO.delete(distributore.getIdDistributore()); return true; } // Elimina un accesso x Utente public Boolean delete(Utente utente) { logger.debug("MakeAccessService: - eliminazione di un accesso x Utente"); utenteDAO.delete(utente.getIdUtente()); return true; } }


Reply With Quote