私はこの質問を検索しましたが、ここではStackOverflowとGoogleにかなりの数がありますが、私にとっては何もできないようです。ここSpring @Transactionはスローされた例外をロールバックしません
は私のコードは 春の設定です:(私は任意のポイントカットを使用いけない - 私は私がする必要がいけないと思いますか?)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
私はServiceクラスを持っている:
@Service
public class ServiceImpl implements ServiceInterface
{
/**
* Injected session factory
*/
@Autowired(required=true)
private SessionFactory sessionFactory;
@Autowired(required=true)
private Dao myDao;
/**
* {@inheritDoc}
*/
@Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public void scheduleBlast(BlastParameters blastParameters) throws ServiceException
{
... do bunch of stuff ..
myDao.persist(entity)
if(true)
throw new ServiceException("random error")
}
.. setter methods and other stuff ..
}
とダオクラス:
public class DaoImpl implements DaoInterface
{
@Autowired(required=true)
private SessionFactory sessionFactory
/**
* {@inheritDoc}
*/
@Transactional(propagation=Propagation.MANDATORY)
public void persist(Entity e) throws DaoException
{
try
{
sessionFactory.getCurrentSession().persist(e);
}
catch(Exception ex)
{
throw new DaoException(ex);
}
}
.. setter methods and other stuff ..
}
いくつかの不必要な詳細は削除されます(たとえば、セッターなどがない)。コードは完全に正常に動作します。
上記の私の問題は、スローランダム例外ラインを追加したときに、DAOを介して永続化されるオブジェクトがロールバックされないことです。
私は春3.1を使用して
思考(春3.1上にHibernate 4.0のバグがあったので)3.6休止のですか?
は、デフォルトでAutoCommitモードに設定されていたものJDBCドライバのいずれかを持っていないあなたに
ブレークポイントがヒットした時点で、デバッグを試して、春が終わるところを確認してください。コードは本当に良いことですが、ソースがIDEにリンクされていることを確認してください! – davidfrancis
mysqlエンジンはMyISAMまたはInnoDBを使用していますが、MyISAMはトランザクションをサポートしません。 –
innoDBを使用しています。もう1つの奇妙な部分は、私が知るところです。サービスクラスは、実際にはmyDao1.persist(o1)、myDao2.persist(o2)、o1に依存するo2を持っています。 o2オブジェクトはロールバックされるが、o1オブジェクトは戻されないことがわかった。 –