如果ObservableCollection<CanvasLine>
引发了Foo
,则以下服务无法回滚保留IllegalStateException
对象,尽管我已将MyServiceImpl
标记为@Transactional(rollbackFor = IllegalStateException.class)
。我还尝试将upload
方法标记为@Transactional(rollbackFor = IllegalStateException.class)
,问题仍然存在,如果发生IllegalStateException
,我的数据库状态将会不一致:
@Service
@Transactional
public class MyServiceImpl implements IMyService{
@Autowired
private IFooService fooService;
@Autowired
private IBarService barService;
@Override
@Transactional(rollbackFor = IllegalStateException.class, propagation = Propagation.REQUIRED)
public void upload(Foo foo, Bar bar) throws IllegalStateException{
try{
fooService.addFoo(foo);//foo object is persisted in the DB whether the upload call would succeed or fail (i.e. throws an IllegalStateException)
if(!check(bar))
throw new IllegalStateException("The object bar is not valid");
barService.addBar();
} catch(Exception e){
e.printStackTrace();
}
}
//This method simply validates the input Bar object
private boolean check(Bar bar){
//source code omitted
}
}
@Service
@Transactional
public class FooServiceImpl implements IFooService{
@Autowired
private IFooDao fooDao;
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addFoo(final Foo foo){
fooDao.addFoo(foo);
}
}
@Service
@Transactional
public class BarServiceImpl implements IBarService{
@Autowired
private IBarDao barDao;
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addBar(final Bar bar){
barDao.addBar(bar);
}
}
public class Foo implements Serializable{
private int id;
//source code omitted
}
public class Bar implements Serializable{
private int id;
private Foo foo;
//source code omitted
}
我的DataSource配置:
@Bean(name="dataSource")
public ComboPooledDataSource getDataSource() throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(env.getProperty("db.driverClass"));
dataSource.setJdbcUrl(env.getProperty("db.jdbcUrl"));
dataSource.setUser(env.getProperty("db.user"));
dataSource.setPassword(env.getProperty("db.password"));
dataSource.setMaxPoolSize(50);
dataSource.setMinPoolSize(5);
dataSource.setMaxConnectionAge(1800);
dataSource.setMaxIdleTime(1800);
dataSource.setAutoCommitOnClose(false);
dataSource.setInitialPoolSize(5);
return dataSource;
}
我该如何解决这个问题?
更新:更新了源代码并添加了try/catch
子句。
答案 0 :(得分:2)
看起来addFoo
在自己的转换中执行。
您可以使用
对其进行注释@Transactional(propagation = Propagation.REQUIRED)
因此在这种情况下它将使用相同的事务,并且如果在没有@Transactional注释的情况下从其他地方调用,则创建一个新事务。
答案 1 :(得分:0)
我花了一段时间才发现回滚失败的原因。感谢ben75的答案。实际上,我提供的源代码并不完整。我忘了在try/catch
方法中添加upload
子句。尽管引发了IllegalStateException
,但这确实阻止了事务被回滚。除此之外,我不需要指定传播级别,因为它默认为REQUIRED
。