Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.query;

import jakarta.persistence.Query;

import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAQuery;
import org.apache.openjpa.persistence.query.common.apps.QTimeout;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;

/**
* Tests that Query.setTimeout(null) clears a timeout set through that method.
*/
public class TestQueryTimeoutClear extends SingleEMFTestCase {

private static final String JPQL = "SELECT o FROM QTimeout o";

@Override
public void setUp() throws Exception {
super.setUp(QTimeout.class, CLEAR_TABLES);
}

public void testSetTimeoutIsReported() {
OpenJPAEntityManager em = emf.createEntityManager();
try {
Query q = em.createQuery(JPQL);
q.setTimeout(5000);
assertEquals(Integer.valueOf(5000), q.getTimeout());
assertEquals(5000, ((OpenJPAQuery<?>) q).getFetchPlan().getQueryTimeout());
} finally {
em.close();
}
}

public void testNullTimeoutClearsPreviousValue() {
OpenJPAEntityManager em = emf.createEntityManager();
try {
Query q = em.createQuery(JPQL);
q.setTimeout(5000);
q.setTimeout(null);
assertNull("A cleared timeout must not report the previous value",
q.getTimeout());
assertEquals("The query must fall back to what it inherits",
em.getFetchPlan().getQueryTimeout(),
((OpenJPAQuery<?>) q).getFetchPlan().getQueryTimeout());
} finally {
em.close();
}
}

public void testNullTimeoutRestoresInheritedValue() {
OpenJPAEntityManager em = emf.createEntityManager();
try {
em.getFetchPlan().setQueryTimeout(9000);
Query q = em.createQuery(JPQL);
q.setTimeout(5000);
assertEquals(Integer.valueOf(5000), q.getTimeout());

q.setTimeout(null);
assertEquals("Clearing must restore the entity manager's timeout, "
+ "not the configuration default",
Integer.valueOf(9000), q.getTimeout());
} finally {
em.close();
}
}

public void testNullTimeoutOnAFreshQueryIsHarmless() {
OpenJPAEntityManager em = emf.createEntityManager();
try {
Query q = em.createQuery(JPQL);
q.setTimeout(null);
assertNull(q.getTimeout());
assertEquals(0, q.getResultList().size());
} finally {
em.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,20 +780,28 @@ public TypedQuery<X> setCacheStoreMode(CacheStoreMode cacheStoreMode) {

@Override
public Integer getTimeout() {
Object val = getHints().get(JPAProperties.QUERY_TIMEOUT);
if (val instanceof Integer) {
return (Integer) val;
}
if (val instanceof Number) {
return ((Number) val).intValue();
}
return null;
int timeout = getFetchPlan().getQueryTimeout();
return timeout > 0 ? timeout : null;
}

/**
* Sets the query timeout in milliseconds. A null timeout clears a timeout
* set through this method, restoring the timeout this query inherits from
* its entity manager, rather than leaving the previous value in place.
*/
@Override
public TypedQuery<X> setTimeout(Integer timeout) {
setHint(JPAProperties.QUERY_TIMEOUT, timeout);
setHint(JPAProperties.QUERY_TIMEOUT,
timeout != null ? timeout : inheritedQueryTimeout());
return this;
}

/**
* The query timeout this query would use had setTimeout() never been
* called on it, i.e. the one configured on its entity manager.
*/
private int inheritedQueryTimeout() {
return _em.getFetchPlan().getQueryTimeout();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,20 @@ public StoredProcedureQuery setCacheStoreMode(CacheStoreMode cacheStoreMode) {
return this;
}

/**
* Sets the query timeout in milliseconds. A null timeout clears a timeout
* set through this method, restoring the timeout this query inherits from
* its entity manager, rather than leaving the previous value in place.
*/
@Override
public StoredProcedureQuery setTimeout(Integer timeout) {
setHint(JPAProperties.QUERY_TIMEOUT, timeout);
_delegate.setTimeout(timeout);
return this;
}

@Override
public Integer getTimeout() {
Object val = getHints().get(JPAProperties.QUERY_TIMEOUT);
if (val instanceof Integer) {
return (Integer) val;
}
if (val instanceof Number) {
return ((Number) val).intValue();
}
return null;
return _delegate.getTimeout();
}

@Override
Expand Down
Loading