我正在使用这个将表从一个模式复制到另一个模式的Spring 4.0服务。我通过在所有DAO继承的抽象类中更新数据源来以编程方式在数据源之间切换。
public abstract class GenericDao implements SystemChangedListener {
private static final Logger logger = Logger.getLogger(GenericDao.class);
private NamedParameterJdbcTemplate jdbcTemplate;
/**
* Initializing the bean with the definition data source through @Autowired
* @param definitionDataSource as instance of @DataSource
*/
@Autowired
private void setDataSource(DataSource definitionDataSource) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(definitionDataSource);
}
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(){
return this.jdbcTemplate;
}
@Override
public void updateDataSource(DataSource dataSource) {
this.setDataSource(dataSource);
}
每当我需要切换到其他DataSource时,我都会调用此方法。到目前为止一切正常,除了我在外键关系中使用外键定义为可延迟的父子表的情况。我得到了#ORA-02292:完整性约束 - 儿童记录'应用程序尝试首先删除父表时找到。
通过调用以下方法在服务中发生这种情况:
logger.info("Switching to target system: " + repDTO.getSourceSystem());
systemService.switchSystem(targetSys);
logger.info("Cleaning up data in target table: " + repDTO.getTableToReplicate());
// deleting the data
managedTableService.cleanData(repDTO.getTableToReplicate());
logger.info("Importing data in target table: " + repDTO.getTableToReplicate());
// importing the data
managedTableService.importData(inserts);
方法在事务中执行,所以当出现问题时,我会返回到clean状态。在复制之前,我还使用Hibernate来收集有关我的表的元信息,但为此我使用了不同的连接。
我试图在清洁方法之前调用SET CONSTRAINT ALL DEFERRED,但它不起作用。我甚至尝试过批量更新 - SET CONSTRAINT ALL DEFERRED + DELETE FROM TABLE,仍然没有成功。我究竟做错了什么?任何帮助表示赞赏。谢谢!
PS。我使用Oracle 11作为数据库。这是我的spring数据源配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Scans within the base package of the application for @Components to configure as beans -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/db.properties" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="de.telekom.cldb.admin"
p:dataSource-ref="dataSource"
p:jpaPropertyMap-ref="jpaPropertyMap"
p:jpaVendorAdapter-ref="hibernateVendor" />
<bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${db.dialect}" />
</bean>
<!-- system 'definition' data source -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${db.driver}"
p:url="${db.url}"
p:username="${db.username}"
p:password="${db.password}" />
<!--
p:maxActive="${dbcp.maxActive}"
p:maxIdle="${dbcp.maxIdle}"
p:maxWait="${dbcp.maxWait}"/>
-->
<util:map id="jpaPropertyMap">
<entry key="generateDdl" value="false"/>
<entry key="hibernate.hbm2ddl.auto" value="validate"/>
<entry key="hibernate.dialect" value="${db.dialect}"/>
<entry key="hibernate.default_schema" value="${db.schema}"/>
<entry key="hibernate.format_sql" value="false"/>
<entry key="hibernate.show_sql" value="true"/>
<entry key="hibernate.connection.SetBigStringTryClob" value="true"/>
<entry key="hibernate.c3p0.min_size" value="5" />
<entry key="hibernate.c3p0.max_size" value="20" />
<entry key="hibernate.c3p0.timeout" value="300" />
<entry key="hibernate.c3p0.max_statements" value="50" />
<entry key="hibernate.c3p0.idle_test_period" value="3000" />
</util:map>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
为了在程序上切换DataSources,我使用org.apache.commons.dbcp.BasicDataSource
########更新