81,092
社区成员
发帖
与我相关
我的任务
分享
<bean id="dataSourceLocal" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="dsLocal" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">${jdbc.local.url}</prop>
<prop key="user">${jdbc.local.username}</prop>
<prop key="password">${jdbc.local.password}</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="testQuery" value="select 1" />
<property name="maintenanceInterval" value="60" />
</bean>
<bean id="dataSourceRemote" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="dsRemote" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">${jdbc.remote.url}</prop>
<prop key="user">${jdbc.remote.username}</prop>
<prop key="password">${jdbc.remote.password}</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="testQuery" value="select 1" />
<property name="maintenanceInterval" value="60" />
</bean>
<!-- 多数据源配置 -->
<bean id="dynamicDataSource" class="com.ht.ourally.common.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSourceLocal" key="dataSourceLocal"></entry>
<entry value-ref="dataSourceRemote" key="dataSourceRemote"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceRemote"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="entityInterceptor" ref="dorado.unByteCodeProxyInterceptor" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.ht.ourally.*.entity</value>
<value>com.ht.mobile.*.entity</value>
<value>com.ht.alipay.entity</value>
</list>
</property>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown">
<value>true</value>
</property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<!-- spring 事务管理器 -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception,RuntimeException" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.ht.ourally.common.task..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- 使用annotation定义事务,对于要加入事物的类,只需对该类加 @Transactional -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<context:annotation-config />
<context:component-scan base-package="com.ht" />
<task:annotation-driven/>
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DBContextHolder.getCustomerType();
}
}
public class DBContextHolder {
public static final String DATASOURCE_LOCAL = "dataSourceLocal";
public static final String DATASOURCE_REMOTE = "dataSourceRemote";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
@Scheduled(cron = "*/5 * * * * ?")
public void main() {
System.out.println("==========================抓取远程数据库数据开始");
try {
this.doBaseCompany();
System.out.println("==========================抓取远程数据库数据结束");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: doBaseCompany
* @Description: TODO 物业企业任务
* @author lindapeng
* @return
*/
private boolean doBaseCompany() {
if (!Boolean.valueOf(prop.get("BaseCompany").toString()))
return true;
System.err.println(DBContextHolder.getCustomerType());
DBContextHolder.setCustomerType(DBContextHolder.DATASOURCE_LOCAL); // 切换数据源到本地
System.err.println(DBContextHolder.getCustomerType());
SysRole role = new SysRole();
role.setCompanyId("lindp1");
role.setRoleName("lindp1");
sysRoleService.addSysRole(role);
System.err.println(DBContextHolder.getCustomerType());
DBContextHolder.setCustomerType(DBContextHolder.DATASOURCE_REMOTE); // 切换数据源到远程
System.err.println(DBContextHolder.getCustomerType());
SysRole role2 = new SysRole();
role2.setCompanyId("lindp2");
role2.setRoleName("lindp2");
sysRoleService.addSysRole(role2);
return true;
}
@Component
public class TakeRemoteTask extends BaseAction {
@Resource
private SessionFactory sessionFactory; // 注入SessionFactory
private static Properties prop = new Properties();
static {
try {
FileInputStream fileInputStream = new FileInputStream(new File(TakeRemoteTask.class.getResource("/").getPath() + "/take.properties"));
prop.load(fileInputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: main
* @Description: TODO 抓取远程数据库数据主任务
* @author lindapeng
*/
@Scheduled(cron = "*/5 * * * * ?")
public void main() {
System.out.println("==========================抓取远程数据库数据开始");
try {
this.doBaseCompany();
System.out.println("==========================抓取远程数据库数据结束");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: doBaseCompany
* @Description: TODO 物业企业任务
* @author lindapeng
* @return
* @throws Exception
*/
private boolean doBaseCompany() throws Exception {
if (!Boolean.valueOf(prop.get("BaseCompany").toString()))
return true;
Session session = null;
Transaction transaction = null;
Session sessionR = null;
Transaction transactionR = null;
try {
DBContextHolder.setCustomerType(DBContextHolder.DATASOURCE_LOCAL); // 切换本地DataSource
session = sessionFactory.openSession(); // 打开一个本地Session
transaction = session.beginTransaction();
SysRole role1 = new SysRole();
role1.setCompanyId("lindp1");
role1.setRoleName("lindp1");
session.save(role1);
DBContextHolder.setCustomerType(DBContextHolder.DATASOURCE_REMOTE); // 切换远程DataSource
sessionR = sessionFactory.openSession(); // 打开一个远程Session
transactionR = sessionR.beginTransaction();
SysRole role2 = new SysRole();
role2.setCompanyId("lindp2");
role2.setRoleName("lindp2");
sessionR.save(role2);
// 同时提交
transaction.commit();
transactionR.commit();
} catch (Exception e) {
// 同时回滚
transaction.rollback();
transactionR.rollback();
e.printStackTrace();
} finally {
session.close();
sessionR.close();
}
// System.err.println(roleService.findSysRoleListByMap(null).size());
//
// DBContextHolder.setCustomerType(DBContextHolder.DATASOURCE_REMOTE);
// System.err.println(roleService.findSysRoleListByMap(null).size());
return true;
}
}