问题描述
我正在尝试一个小程序,我可以使用Spring连接到多个数据库,并尝试通过在weblogic服务器上部署我的Web应用程序来使用Spring事务。问题是事务管理不能正常工作。我试图在2个数据库中插入记录,第一个插入而不抛出任何异常,其中第二个插入查询被编写为抛出异常。理想情况下,在这种情况下,事务应该回滚,但第一个事务提交没有任何问题。
I am trying to work on a small program where I can connect to multiple databases using Spring and trying to use Spring transactions by deploying my web-application on weblogic server. The problem is that the transaction management is not working properly. I am trying to insert records in 2 databases, the first one inserts without throwing any exceptions, where as the second insert query is written such that it throws an exception. Ideally in this situation the transaction should rollback but the first transaction is committed without any issues.
这是我的 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
EmployeeDetailsService.java -- The DAO in this service throws NullPointerException
@Service
public class EmployeeDetailsService {
@Autowired
EmployeeDetailsDao dao;
//@Transactional(propagation=Propagation.REQUIRED)
public boolean insert(EmployeeDetails e) {
return dao.insert(e);
}
}
更新:添加Dao类:
EmployeeDao.java:
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
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;
}
}
推荐答案
尝试在中添加以下内容:spring-config.xml
:
<context:component-scan base-package="your.packagename.contatining.EmployeeDetailsDao" />
<mvc:annotation-driven />
并在<$中更改 @Transactional
c $ c> CommonEmployeeService with @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
如下所示
and changing @Transactional
in your CommonEmployeeService
with @Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
as below
@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;
}
}
这篇关于通过连接到多个数据库进行Spring事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!