我想在服务类中连续插入两个不同的表,我使用的是Spring JDBCTemplate。我的代码是。
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
public boolean insertRestorent(User user) throws GenericException {
logger.println(IMessage.INFO, new StringBuilder(CLASS_NAME).append("::insertRestorent() restorent registration start"));
user.setUserType(USER_TYPE.RESTORENT.ID);
User checkUser = userDao.checkUserByEmail(user.getEmail());
if (checkUser != null) {
GenericException exception = new GenericException();
exception.setMessage("Shop already registered!!");
throw exception;
}
long getuserUid=userDao.insertUser(user);
long retoId= restorentdao.insertRestorent(user, getuserUid);
return false;
}
我的transactionManager代码是
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" />
</bean>
我的问题是userDao.insertUser(用户);是成功但如果在restorentdao.insertRestorent(user,getuserUid)中有任何异常;然后拳头用户表不回滚。 例外是。
SEVERE:servlet [MyDelivery]的Servlet.service()在路径[/ MyDelivery]的上下文中引发异常[请求处理失败;嵌套异常是org.springframework.dao.DataIntegrityViolationException:PreparedStatementCallback; SQL [];字段'shop_description'没有默认值;嵌套异常是java.sql.SQLException:字段'shop_description'没有默认值]有根本原因 java.sql.SQLException:字段'shop_description'没有默认值
userDao insert方法是。
public long insertUser(User user) {
logger.println(IMessage.INFO, new StringBuilder(CLASS_NAME).append("::insertUser()"));
final String query = "INSERT INTO user(user_type,email,mobile_no,password,name,status,created_on)VALUES(?,?,?,?,?,?,now());";
KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemplate().update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
int i = 1;
ps.setLong(i++, user.getUserType());
ps.setString(i++, user.getEmail());
ps.setString(i++, user.getMobileNo());
ps.setString(i++, user.getPassword());
if (!StringUtils.isEmpty(user.getName())) {
ps.setString(i++, user.getName());
} else {
ps.setString(i++, null);
}
ps.setInt(i++, STATUS.INACTIVE.ID);
return ps;
}
}, keyHolder);
user.setUserId(keyHolder.getKey().longValue());
return keyHolder.getKey().longValue();
}