我正在尝试使用spring数据jpa + eclipselink配置多个数据源,似乎使用jta是我唯一的选择,经过对此主题的大量研究后,我得到了以下输出,表示我试图将两个实体插入来自不同db - department表和employee表的两个表。查询很好,但插入操作不起作用而没有抛出错误。
请看看我的代码
APP-context.xml中:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--<jee:jndi-lookup jndi-name="test" id="dataSource" resource-ref="true"/>-->
<bean abstract="true" id="baseDS" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" lazy-init="true">
<property name="pinGlobalTxToPhysicalConnection" value="true" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<bean id="dataSource1" parent="baseDS">
<property name="url">
<value>${jdbc1.url}</value>
</property>
</bean>
<bean id="dataSource2" parent="baseDS">
<property name="url">
<value>${jdbc2.url}</value>
</property>
</bean>
<bean id="baseJTADS" class="com.atomikos.jdbc.AtomikosDataSourceBean" abstract="true" init-method="init" destroy-method="close">
<property name="poolSize" value="3" />
</bean>
<bean id="jtaDs1" parent="baseJTADS">
<property name="uniqueResourceName" value="XA1DBMS" />
<property name="xaDataSource" ref="dataSource1" />
</bean>
<bean id="jtaDs2" parent="baseJTADS">
<property name="uniqueResourceName" value="XA2DBMS" />
<property name="xaDataSource" ref="dataSource2" />
</bean>
<bean abstract="true" id="baseEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="org.fxbird.springjpa.model"/>
<property name="jpaProperties">
<props>
<prop key="databasePlatform">org.eclipse.persistence.platform.database.MySQLPlatform</prop>
<prop key="eclipselink.jdbc.cache-statements">true</prop>
<prop key="eclipselink.weaving">false</prop>
<prop key="eclipselink.logging.level">fine</prop>
<prop key="eclipselink.allow-zero-id">true</prop>
</props>
</property>
</bean>
<bean id="emfCompany" parent="baseEmf">
<!--<property name="persistenceUnitName" value="company" />-->
<property name="dataSource" ref="jtaDs1"/>
</bean>
<bean id="emfEmp" parent="baseEmf">
<!--<property name="persistenceUnitName" value="employee" />-->
<property name="dataSource" ref="jtaDs2"/>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
destroy-method="close">
<property name="forceShutdown" value="true"/>
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction"/>
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<context:component-scan base-package="org.fxbird.springjpa.service"/>
<jpa:repositories base-package="org.fxbird.springjpa.repo.company"
entity-manager-factory-ref="emfCompany" transaction-manager-ref="transactionManager">
</jpa:repositories>
<jpa:repositories base-package="org.fxbird.springjpa.repo.emp"
entity-manager-factory-ref="emfEmp" transaction-manager-ref="transactionManager">
</jpa:repositories>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:annotation-config/>
</beans>
DeptRepo:
@Repository()
public interface DeptRepo extends JpaRepository<Department,Integer> {
}
EmployeeRepo:
public interface EmployeeRepo extends JpaRepository<Employee,Integer> {
}
服务:
@Service
public class DepartmentService {
@Autowired
DeptRepo deptRepo;
@Autowired
EmployeeRepo employeeRepo;
@Transactional
public void addDepartment(Department department){
deptRepo.save(department);
}
public List<Department> findAllDepartments(){
return deptRepo.findAll();
}
@Transactional
public void addEmployee(Employee employee){
employeeRepo.save(employee);
}
}
测试类:
public class App {
public static void main(String[] args) {
GenericXmlApplicationContext context=new GenericXmlApplicationContext("/app-context.xml");
DepartmentService departmentService=(DepartmentService)context.getBean("departmentService");
Department department=new Department();
department.setCreateDate(new Date());
department.setDeptName("HR");
departmentService.addDepartment(department); //not working at all!!!
List<Department> allDepartments = departmentService.findAllDepartments();
System.out.println(allDepartments.size());
Employee employee=new Employee();
employee.setEmpName("Kurt");
employee.setDeptNo(department.getDeptNo());
departmentService.addEmployee(employee); //not working at all!!!
}
}
有两个db - 公司和员工分别有表部门和员工,结构如下:
CREATE TABLE `department` (
`dept_no` int(11) NOT NULL AUTO_INCREMENT,
`dept_name` varchar(255) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
PRIMARY KEY (`dept_no`)
)
CREATE TABLE `employee` (
`emp_no` int(11) NOT NULL AUTO_INCREMENT,
`emp_name` varchar(255) DEFAULT NULL,
`dept_no` int(11) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
PRIMARY KEY (`emp_no`)
)
任何人都可以对此有所了解吗?我很难搞清楚原因,谢谢
库尔特