java如何多表断网,java Web如何离线使用并进行数据同步

你参考下JBOSS XA数据源

standalone.xml

jdbc:h2:mem:test;DB_CLOSE_DELAY=-1

h2

sa

sa

localhost

MYDB_ONE

cursor

com.microsoft.sqlserver.jdbc.SQLServerXADataSource

sqljdbc

false

some_user

some_password

localhost

MYDB_TWO

cursor

com.microsoft.sqlserver.jdbc.SQLServerXADataSource

sqljdbc

false

some_user

some_password

org.h2.jdbcx.JdbcDataSource

com.microsoft.sqlserver.jdbc.SQLServerDriver

org.postgresql.xa.PGXADataSource

springJpaConfig.xml

DatasourceRegisteringBeanFactoryPostProcessor.java

package some.package

class DatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {

HashMap connectionsListMyDB = new HashMap<>();

// Load your connection list from wherever you need to, you can

// enumerate them directly from JNDI or some configuration location

connectionsListMyDB.put("db1", "java:jboss/datasources/MYDB_ONE");

connectionsListMyDB.put("db2", "java:jboss/datasources/MYDB_TWO");

if (connectionsList.isEmpty())

throw new RuntimeException("No JPA connections defined");

// Configure the dataSource bean properties

BeanDefinitionRegistry factory = (BeanDefinitionRegistry) beanFactory;

MutablePropertyValues mpv = factory.getBeanDefinition("dataSourceMyDB").getPropertyValues();

ManagedMap mm = (ManagedMap) mpv.getPropertyValue(

"targetDataSources").getValue();

mm.clear();

for (Entry e : connectionsListMyDB.entrySet()) {

mm.put(e.getKey(), e.getValue());

}

}

}

AbstractRoutingDataSourceMyDB.java

public class AbstractRoutingDataSourceMyDB extends AbstractRoutingDataSource {

@Override

protected Object determineCurrentLookupKey() {

return getDbConnectionMyDB();

}

// ThreadLocal variable so that the connection gets set for the current thread

// using spring""s request scope on the class instead of ThreadLocal would also work here.

private final ThreadLocal contextHolder = new ThreadLocal();

public void setDbConnectionMyDB(String myKey) {

Assert.notNull(myKey, "myKey cannot be null");

contextHolder.set(myKey);

String k = contextHolder.get();

}

public String getDbConnectionMyDB() {

return (String) contextHolder.get();

}

public void clearDbConnectionMyDB() {

contextHolder.remove();

}

}

SomeTableDAO.java

@PersistenceContext(unitName = "MyDB")

private EntityManager em;

@Autowired

private AbstractRoutingDataSourceMyDB routingSource;

public void someMethod(int id) {

em.flush();

em.clear();

routingSource.setDbConnectionMyDB("db1");

em.remove(em.getReference(Something.class, id)); // delete something in db1

em.flush();

em.clear();

routingSource.setDbConnectionMyDB("db2");

em.remove(em.getReference(Something.class, id)); // delete something else with the same id in db2

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值