I use Spring MVC without web.xml because I use last Spring Security config so I have a MvcConfig class like this :
@Configuration
@EnableTransactionManagement
@ComponentScan("com.atoutjeu")
public class MvcConfig implements TransactionManagementConfigurer {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "dataSource")
public DataSource dataSource() {
ResourceBundle dbBundle = ResourceBundle.getBundle("database");
dbBundle.getString("db.ip");
DriverManagerDataSource driverManagerDataSource = new TransactionalDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgresql://"+dbBundle.getString("db.ip")+":5432/"+dbBundle.getString("db.dbname"));
driverManagerDataSource.setUsername(dbBundle.getString("db.user"));
driverManagerDataSource.setPassword(dbBundle.getString("db.password"));
return driverManagerDataSource;
}
@Bean(name="txManager")
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return transactionManager();
}
@Bean(name = "resourceBundleViewResolver")
public ResourceBundleViewResolver getResourceBundleViewResolver(){
ResourceBundleViewResolver rbvr = new ResourceBundleViewResolver();
rbvr.setOrder(0);
rbvr.setBasename("views");
return rbvr;
}
@Bean
public PretServiceImpl pretServiceImpl() {
// configure and return a class having @Transactional methods
return new PretServiceImpl();
}
}
And I have a service :
@Service
@Transactional
public class PretServiceImpl implements IPretService {
@Autowired
private IAdherentDao adherentDao;
@Autowired
private IJeuDao jeuDao;
@Autowired
private IPretDao pretDao;
@Autowired
private IOperationDao operationDao;
@Autowired
private IParamLudoDao paramLudoDao;
private Logger logger = Logger.getLogger(AdherentServiceImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public int louerPaiementDirect(int idAdherent, int idJeu, String dateSortie, String dateRetourPrevue, String commentaireSortie) throws ExceptionMetier, ExceptionTechnique {
Adherent adherent = null;
try {
adherent = adherentDao.getAdherent(idAdherent);
} catch (ExceptionTechnique e) {
// TODO: handle exception
}
Jeu jeu = jeuDao.getJeu(idJeu);
Pret pret = new Pret();
pret.setAdherent(adherent);
pret.setJeu(jeu);
pret.setDateSortie(DateUtils.getDateFromDatePicker(dateSortie));
pret.setDateRetourPrevue(DateUtils.getDateFromDatePicker(dateRetourPrevue));
pret.setCommentaireSortie(commentaireSortie);
int idPret = pretDao.creerPret(pret);
//throw new RuntimeException();
//TODO créer une opération
Operation operation = new Operation();
operation.setAdherent(adherent);
operation.setDateOperation(DateUtils.getDateFromDatePicker(dateSortie));
operation.setDateReglement(DateUtils.getDateFromDatePicker(dateSortie));
operation.setModeReglement(paramLudoDao.getParamLudo(ModeReglement.class, "mode_reglement", ModeReglement.TIRELIRE));
operation.setPrestation(jeu.getCategorie().getPrestation());
operation.setMontant(operation.getPrestation().getCout());
operation.setCredit(false);
//operation.setTauxRemise(tauxRemise);
operationDao.creerOperation(operation);
adherent.setTirelire(adherent.getTirelire() - jeu.getCategorie().getPrestation().getCout());
return idPret;
}
//....
}
When an exception appear my methode louerPaiementDirect doesn't roll back. I don't understand why ?
Is it unchecked exception (RuntimeException or it subtype) or checked one.
Spring Framework's @transaction rollback only in the case of runtime, unchecked exceptions. You can though get rollback for checked exception with some change in your config.
Please find the below code for web.xml alternative
package com.candidjava.spring.configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.candidjava.spring.configuration.SpringConfiguration;
public class SpringWebIntializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
// TODO Auto-generated method stub
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringConfiguration.class);
context.setServletContext(container);
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.