我有2个用于处理JDBC和Sqlite的类。这些类使用抽象类来扩展:
package atm.implementations;
import atm.dao.SQLiteDAO;
import atm.objects.Bank;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component("bankDAO")
public class BankDAO extends AbstractDAO implements SQLiteDAO<Bank> {
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public void updateRecord(Bank object) {
String sqlUpdate = "update bank set account_value = :value where id = :id";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("value", object.getAccountValue());
params.addValue("id", object.getId());
jdbcTemplate.update(sqlUpdate, params);
}
}
头等舱
package atm.implementations;
import atm.dao.TranDAO;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Component("transDAO")
public class TranDAOImpl extends AbstractDAO implements TranDAO {
@Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.SERIALIZABLE)
public void insert(String desc) {
String sqlInsert = "insert into transactions_list (description) values (:descr)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("descr", desc);
jdbcTemplate.update(sqlInsert, params);
}
}
第二:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="atm.*"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value=""/>
<property name="password" value=""/>
<property name="url" value="jdbc:sqlite:atm.db"/>
<property name="driverClassName" value="org.sqlite.JDBC"/>
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
这是context.xml:
$('#_customize-input-checkin\\[message\\]').focus(function() {
var $iframe = $("#customize-preview iframe").contents();
$("#alert_check_in_notification_sent", $iframe).css("display", "block");
$("#check-in-wrap", $iframe).css("display", "none");
});
我需要在这两个类中将方法设为事务型,但每个类的数据源都不同。现在我有错误-找不到标记为传播“强制性”的交易的现有交易。
第一类使用新的帐户参数更新数据库,第二类仅在有关此交易的单独表中插入文本。如果交易失败,第二类不应插入任何信息。
我真的不想将所有代码都放在一个类中,我想将其分开。
是否可以对所有类使用一个单一的数据源?
答案 0 :(得分:0)
TranDAOImpl#insert 标有MANDATORY-Transaction-Propagation。如果是这样,则意味着调用者必须具有活动的事务。我怀疑调用此方法时会收到异常消息?
因此,应使用“ REQUIRED”或“ REQUIRES_NEW”对调用者本身进行注释。我认为您的问题与使用不同的数据源无关。如果您的Spring接线正确,则事务管理器应以安全的方式进行处理。