简体   繁体   English

无法获得 JDBC 连接/事务未激活

[英]Could not get JDBC Connection / Transaction is not active

I'm struggling with a call to my database through Spring Jdbc, none of the questions on stack overflow worked for me so that's why I am asking it.我正在努力通过 Spring Jdbc 调用我的数据库,关于堆栈溢出的任何问题都不适合我,所以这就是我问它的原因。

Here's my DAO这是我的 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;
    }

}

Here's the exception it throws when calls the executeFunction line:这是调用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 >)

I have correctly declared the beans for the datasource with the information of the database.我已经使用数据库的信息正确声明了数据源的 bean。 在此处输入图片说明

EDIT The method where I call my DAO was already @Transactional编辑我调用我的 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;
        }

        ...

What I am missing?我缺少什么? Thanks in advise.谢谢指教。

You need to have an active transaction while accessing the db.访问数据库时,您需要有一个活动事务。 You have defined the transaction manager bean in your xml, but not using it.You need to annotate the service method @Transactional who is calling this obtainCode method.你已经在你的xml中定义了事务管理器bean,但没有使用它。你需要注释调用这个obtainCode方法的服务方法@Transactional Also note that, @Transactional annotated method needs to be public and called from another bean(due to proxying mechanism), to make it work properly.另请注意,@ @Transactional注释方法需要公开并从另一个 bean 调用(由于代理机制),以使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM