[QueryDSL/Spring]java.lang.IllegalStateException:连接不是事务性的 [英] [QueryDSL/Spring]java.lang.IllegalStateException: Connection is not transactional

查看:67
本文介绍了[QueryDSL/Spring]java.lang.IllegalStateException:连接不是事务性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 Spring+Vaadin 应用程序.我想添加 QueryDSL 来访问数据库(Oracle).我查看了文档(http://docs.spring.io/spring-data/jdbc/docs/current/reference/html/core.querydsl.html) 并且我读到 Spring 建议使用标准 QueryDSL api.我将以下依赖项导入到我的项目中:

I writing Spring+Vaadin application. I wanted to add QueryDSL to access db (Oracle). I looked in documentation (http://docs.spring.io/spring-data/jdbc/docs/current/reference/html/core.querydsl.html) and I read that Spring recommend using standard QueryDSL api. I improted to my project following dependecies:

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-sql-spring</artifactId>
    <version>${querydsl.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-sql</artifactId>
    <version>${querydsl.version}</version>
</dependency>

我的beans.xml如下:

<bean id="dataSourceOracle" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.oracle.driverClassName}" />
    <property name="url" value="${db.oracle.url}"/>
    <property name="username" value="${db.oracle.username}" />
    <property name="password" value="${db.oracle.password}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

在我的 DatabaseFacade 实现中,我做了以下配置:

In my DatabaseFacade implementation I do following configuration:

private SQLQueryFactory query;

@Autowired
@Qualifier("DataSource")
public void setDataSource(DataSource dataSource) {
    Provider<Connection> provider = new SpringConnectionProvider(dataSource);
    Configuration configuration = new Configuration(new OracleTemplates());
    configuration.setExceptionTranslator(new SpringExceptionTranslator());
    query = new SQLQueryFactory(configuration, provider);
}

不幸的是,每次我开始申请时都会收到:

Unfortunately everytime I start application I got:

10:29:54.490 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
10:29:56.231 [main] ERROR c.r.i.k.b.impl.DatabaseFacadeImpl - Error happend in com.roche.icc.kps.backend.impl.DatabaseFacadeImpl.getEditableKPSStores
10:29:56.234 [main] ERROR c.r.i.k.b.impl.DatabaseFacadeImpl - Connection is not transactional
java.lang.IllegalStateException: Connection is not transactional
    at com.mysema.query.sql.spring.SpringConnectionProvider.get(SpringConnectionProvider.java:45) ~[querydsl-sql-spring-3.7.0.jar:na]
    at com.mysema.query.sql.spring.SpringConnectionProvider.get(SpringConnectionProvider.java:33) ~[querydsl-sql-spring-3.7.0.jar:na]
    at com.mysema.query.sql.SQLQueryFactory.query(SQLQueryFactory.java:63) ~[querydsl-sql-3.7.0.jar:na]
    at com.mysema.query.sql.SQLQueryFactory.query(SQLQueryFactory.java:28) ~[querydsl-sql-3.7.0.jar:na]
    at com.mysema.query.sql.AbstractSQLQueryFactory.from(AbstractSQLQueryFactory.java:54) ~[querydsl-sql-3.7.0.jar:na]

有人遇到过这个问题吗?我应该使用不同的 DataSource (Atomikos?)?

Did anyone encounter this problem? Should I use different DataSource (Atomikos?)?

感谢帮助!

卡米尔

推荐答案

我遇到了同样的问题.

问题是 QueryDSL 期望数据源连接在事务中.所以要么你必须显式地开始事务并处理它,要么 IOC 容器应该为你做.

The issue is that QueryDSL expects the datasource connection to be in a transaction. So either you have to explicitly begin transactions and handle it or the IOC container should do it for you.

在这种情况下,您很可能在任何应该启动事务的应用程序层(服务/存储库)中都没有 @Transactional 注释.

In this case you most probably dont have the @Transactional annotation in any of your application layer (Service / Repository) which should have started a transaction.

向您的 dao 或服务层添加 @Transactional 注释以解决问题.

Add a @Transactional annotation to either your dao or service layer to fix the issue.

帮助我的链接是https://groups.google.com/forum/#!topic/querydsl/_PqMek79TZE

一个示例项目.https://github.com/querydsl/querydsl/tree/master/querydsl-examples/querydsl-example-sql-spring

更新

这就是示例项目中的 DaoImpl.
注意类级别的@Transactional 注解.

This is how the DaoImpl from the example project.
Note the @Transactional annotation in the class level.

@Transactional
public class CustomerDaoImpl implements CustomerDao {

@Inject
SQLQueryFactory queryFactory;

final QBean<CustomerAddress> customerAddressBean = bean(CustomerAddress.class,
        customerAddress.addressTypeCode, customerAddress.fromDate, customerAddress.toDate,
        bean(Address.class, address.all()).as("address"));

final QBean<Customer> customerBean = bean(Customer.class,
        customer.id, customer.name,
        bean(Person.class, person.all()).as("contactPerson"),
        GroupBy.set(customerAddressBean).as("addresses"));

@Override
public Customer findById(long id) {
    List<Customer> customers = findAll(customer.id.eq(id));
    return customers.isEmpty() ? null : customers.get(0);
}

@Override
public List<Customer> findAll(Predicate... where) {
    return queryFactory.from(customer)
        .leftJoin(customer.contactPersonFk, person)
        .leftJoin(customer._customer3Fk, customerAddress)
        .leftJoin(customerAddress.addressFk, address)
        .where(where)
        .transform(GroupBy.groupBy(customer.id).list(customerBean));
}

@Override
public Customer save(Customer c) {
    Long id = c.getId();

    if (id == null) {
        id = queryFactory.insert(customer)
                .set(customer.name, c.getName())
                .set(customer.contactPersonId, c.getContactPerson().getId())
                .executeWithKey(customer.id);
        c.setId(id);
    } else {
        queryFactory.update(customer)
            .set(customer.name, c.getName())
            .set(customer.contactPersonId, c.getContactPerson().getId())
            .where(customer.id.eq(c.getId()))
            .execute();

        // delete address rows
        queryFactory.delete(customerAddress)
            .where(customerAddress.customerId.eq(id))
            .execute();
    }

    SQLInsertClause insert = queryFactory.insert(customerAddress);
    for (CustomerAddress ca : c.getAddresses()) {
        if (ca.getAddress().getId() == null) {
            ca.getAddress().setId(queryFactory.insert(address)
                .populate(ca.getAddress())
                .executeWithKey(address.id));
        }
        insert.set(customerAddress.customerId, id)
            .set(customerAddress.addressId, ca.getAddress().getId())
            .set(customerAddress.addressTypeCode, ca.getAddressTypeCode())
            .set(customerAddress.fromDate, ca.getFromDate())
            .set(customerAddress.toDate, ca.getToDate())
            .addBatch();
    }
    insert.execute();

    c.setId(id);
    return c;
}

@Override
public long count() {
    return queryFactory.from(customer).fetchCount();
}

@Override
public void delete(Customer c) {
    // TODO use combined delete clause
    queryFactory.delete(customerAddress)
        .where(customerAddress.customerId.eq(c.getId()))
        .execute();

    queryFactory.delete(customer)
        .where(customer.id.eq(c.getId()))
        .execute();
}

}

在您的情况下,注释应放置在您的 DatabaseFacade 实现中.我假设您的 Spring 应用程序也配置了一个事务管理器.

In your case the annotation shall be placed in your DatabaseFacade Implementation. I assume that your Spring application has a transaction manager configured as well.

这篇关于[QueryDSL/Spring]java.lang.IllegalStateException:连接不是事务性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆