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>
My beans.xml is as follows:
<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>
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]
Did anyone encounter this problem? Should I use different DataSource
(Atomikos?)?
Thanks for help!
Kamil
I came across with the same issue.
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.
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.
Add a @Transactional annotation to either your dao or service layer to fix the issue.
The links that helped me were https://groups.google.com/forum/#!topic/querydsl/_PqMek79TZE
An example project. https://github.com/querydsl/querydsl/tree/master/querydsl-examples/querydsl-example-sql-spring
Update
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With