【发布时间】:2020-01-14 10:28:40
【问题描述】:
我在通过 Spring Jdbc 调用我的数据库时遇到了困难,关于堆栈溢出的问题都没有对我有用,所以这就是我问它的原因。
这是我的 DAO
public class CruDao extends JdbcDaoSupport implements ICruDao{
private final Logger LOG = LoggerFactory.getLogger(CruDao.class);
@Override
public String obtainCode(String codiModel) {
String code = null;
SimpleJdbcCall funcio = new SimpleJdbcCall(getJdbcTemplate()).withFunctionName("funcCru");
MapSqlParameterSource parameter = new MapSqlParameterSource().addValue("id", codiModel);
try {
code = funcio.executeFunction(String.class, parameter);
LOG.debug("Generated Code: " + code);
}catch(Exception ex){
LOG.error("Error",ex);
}
return codiCRU;
}
}
这是调用executeFunction 行时抛出的异常:
org.springframework.dao.DataAccessResourceFailureException: Error retreiving database metadata; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.jboss.util.NestedSQLException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -3f5796c1:d4ac:5e1c3647:9ff status: ActionStatus.ABORTED >; - nested throwable: (javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -3f5796c1:d4ac:5e1c3647:9ff status: ActionStatus.ABORTED >)
编辑 我调用 DAO 的方法已经是 @Transactional
@Override
@Transactional
public byte[] obtenirModel(String codiModel) {
byte[] genDocByteArray = null;
codiCRU = cruDao.obtainCode(codiModel);
if(codiCRU == null || codiCRU.length() < 1){
log.error("Errorrr");
return null;
}
...
我错过了什么?感谢您的建议。
【问题讨论】:
-
错误告诉你究竟出了什么问题。没有活跃的交易。您定义事务管理器这一事实并不意味着您有事务。您仍然必须将代码/方法标记为事务性的。请将 xml 配置添加为文本而不是图像,后者非常不可读。
-
抱歉,xml 无法读取,顺便说一下,我调用 DAO 的方法已经是事务性的。我的DAO的接口也应该有Transactional注解吗? @M.Deinum
-
在 XML 中添加
@Transactional而没有<tx:annotation-driven />不会使其具有事务性。 -
您是否启用了事务管理(通过注释(配置类上的
@EnableTransactionManagement)或通过xml 配置(<tx:annotation-driven/>)?