我正在尝试一个小程序,我可以使用Spring连接到多个数据库,并尝试通过在weblogic服务器上部署我的Web应用程序来使用Spring事务。问题是事务管理不能正常工作。我试图在2个数据库中插入记录,第一个插入而不抛出任何异常,其中第二个插入查询被编写为抛出异常。理想情况下,在这种情况下,事务应该回滚,但第一个事务是在没有任何问题的情况下提交的。
这是我的 spring-config.xml 文件
<context:component-scan base-package="com.examples" />
<!-- Database1 -->
<bean id="db1DataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb1" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Database2 -->
<bean id="db2DataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb2" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix" value=".jsp" />
</bean>
这是我的控制者:
@Controller
public class EmployeeController {
@Autowired
private CommonEmployeeService commonService;
@RequestMapping(value = "/employee", method = GET)
public String showPersonListForGivenAge(
@RequestParam(value = "id") int id,
@RequestParam(value = "name") String name,
@RequestParam(value = "email") String email,
Map<String, Object> model) {
Employee e = new Employee(id,name);
EmployeeDetails details = new EmployeeDetails(id,email);
commonService.insert(e, details);
return "welcome";
}
}
这是我的共同服务:
@Service
public class CommonEmployeeService {
@Autowired
EmployeeDetailsService detailsService;
@Autowired
EmployeeService service;
@Transactional
public boolean insert(Employee e, EmployeeDetails details) {
service.insert(e);
detailsService.insert(details);
return true;
}
}
这些是我的其他服务:
EmployeeService.java
@Service
public class EmployeeService {
@Autowired
EmployeeDao dao;
//@Transactional(propagation=Propagation.REQUIRED)
public boolean insert(Employee e) {
return dao.insert(e);
}
}
EmployeeDetailsService.java - 此服务中的DAO会抛出NullPointerException
@Service
public class EmployeeDetailsService {
@Autowired
EmployeeDetailsDao dao;
//@Transactional(propagation=Propagation.REQUIRED)
public boolean insert(EmployeeDetails e) {
return dao.insert(e);
}
}
更新:添加Dao类:
EmployeeDao.java:
@Repository
public class EmployeeDao {
JdbcTemplate template;
@Resource(name = "db1DataSource")
public void setDataSource(DataSource dataSource) {
this.template = new JdbcTemplate(dataSource);
}
public boolean insert(Employee e) {
int cnt = template.update("insert into Employee values(?,?)",
e.getId(), e.getName());
if (cnt > 0) {
return true;
}
return false;
}
}
EmployeeDetailsDao.java
@Repository
public class EmployeeDetailsDao {
JdbcTemplate template;
@Resource(name = "db2DataSource")
public void setDataSource(DataSource dataSource) {
this.template = new JdbcTemplate(dataSource);
}
public boolean insert(EmployeeDetails e) {
if(e != null){
throw new NullPointerException();
}
int cnt = template.update("insert into EmployeeDetails values(?,?)",
e.getId(), e.getEmail());
if (cnt > 0) {
return true;
}
return false;
}
}
答案 0 :(得分:0)
尝试在spring-config.xml
中添加以下内容:
<context:component-scan base-package=“your.packagename.contatining.EmployeeDetailsDao” />
<mvc:annotation-driven />
并使用@Transactional
更改CommonEmployeeService
中的@Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
@Service
public class CommonEmployeeService {
@Autowired
EmployeeDetailsService detailsService;
@Autowired
EmployeeService service;
@Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public boolean insert(Employee e, EmployeeDetails details) {
service.insert(e);
detailsService.insert(details);
return true;
}
}