私はempテーブルとdeptテーブルにデータを挿入する世話をする2つのサービスレイヤーを持っていました。春の伝播require_newは機能しません
deptnoは長さ> 2のdeptnoを受け入れることができないため、DataIntegrity違反のためにdbに挿入されません。私は前回の取引がこの取引に依存しないようにします。したがって、REQUIRES_NEW伝播レベルが使用されました。
ここに私のコードです。以下は
@Component
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DeptService deptService;
@Transactional(propagation = Propagation.REQUIRED)
public boolean createEmployee() {
employeeDao.insertEmployee();
deptService.createDept();
return false;
}
}
@Component
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDao deptDao;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean createDept() {
deptDao.insertDepartment();
return false;
}
}
ダオ層
@Component
public class EmployeeDaoImpl implements EmployeeDao {
private String empInsert = "INSERT INTO emp VALUES ('1000','Ravi','CLERK','7782','1990-01-01','1235.00',NULL,'50');";
@Autowired
private JdbcTemplate jdbcTemplate;
public boolean insertEmployee() {
int n = jdbcTemplate.update(empInsert);
return true;
}
}
@Component
public class DeptDaoImpl implements DeptDao {
private String deptInsert = "INSERT INTO dept VALUES ('500','MATERIAL','ALASKA');";
@Autowired
private JdbcTemplate jdbcTemplate;
public boolean insertDepartment(){
jdbcTemplate.update(deptInsert);
return true;
}
}
は、次のコードは、以下
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("txrequirednew.xml");
EmployeeService es = (EmployeeService) context.getBean("employeeServiceImpl");
es.createEmployee();
}
私のアプリケーションのコンテキストでメインクラスから呼び出されます:
<beans>
<context:annotation-config/>
<context:component-scan base-package="txrequirednew">
</context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/tejadb"></property>
<property name="username" value="root"/>
<property name="password" value="sai"/>
</bean>
<!-- Dao Configurations-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
私がしよう実行する、両方のtransa ctionsはロールバックです。しかし、私は最初のトランザクションをロールバックしたくありません。
私はここにいなくなり、両方のトランザクションでロールバックが発生していますか? これをどのように修正できますか?
ありがとうA.Rアフシャリアン。それは私のために働いた。 – user3777919