连接未与托管连接关联

时间:2018-11-03 22:19:47

标签: java spring jpa spring-data-jpa

使用以下代码进行JPA查询时出现异常:

public Collection<Merchants> findAll() {
    String hql = "select e from " + Merchants.class.getName() + " e";
    TypedQuery<Merchants> query = entityManager.createQuery(hql, Merchants.class);
    List<Merchants> merchants = query.getResultList();
    return merchants;
}

错误异常:

23:50:47,654 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) SQL Error: 0, SQLState: null
23:50:47,656 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) IJ031040: Connection is not associated with a managed connection: org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8@31c8db05
23:50:47,659 ERROR [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-1) Forwarding to error page from request [/merchants/list] due to exception [org.hibernate.exception.GenericJDBCException: could not execute query]: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
    at deployment.datalis_admin.war//org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)

完整日志:

https://pastebin.com/zGVNZVUY

你能建议我哪里错了吗?

Java配置:

@Configuration
@EnableTransactionManagement
public class ContextDatasource {

    @Bean
    public EntityManager entityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }
}

Application.properties配置

spring.jmx.enabled=false
spring.datasource.jndi-name=java:/global/production_gateway
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
request.limit=300000

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=*
spring.mail.password=*
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

spring.data.rest.basePath=/api
server.servlet.context-path=/api
spring.jackson.default-property-inclusion=non_null

app.security.token-key='key123'
app.email.sending-enabled=false
app.email.reset.subject=Password reset subject
app.email.reset.content=Go to the link provided to reset your password: %s

app.reset-url=/reset?token=
app.url=localhost:4200

logging.level.org.springframework.web=ERROR
logging.level.com.backend=DEBUG
logging.file=${java.io.tmpdir}/application.log

您知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

由于配置没有问题,只有下拉菜单中有此异常,我想是将您的代码更改为此:

private List<Merchants> merchants;

public List<Merchants> getMerchants() {
     //this is going to run so many times, so prevent extra queries
    if(merchants == null)
    {
        merchants = merchantService.findAll();
    {    
    return merchants;
}

MerchantService.java

@Transactional(readonly=true)
public Collection<Merchants> findAll() {
    String hql = "select e from " + Merchants.class.getName() + " e";
    TypedQuery<Merchants> query = entityManager.createQuery(hql, Merchants.class);
    List<Merchants> merchants = query.getResultList();
    return merchants;
}