Author: epbernard
Date: 2008-08-14 15:03:46 -0400 (Thu, 14 Aug 2008)
New Revision: 15080
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/QueryImpl.java
Log:
EJB-375 getSingleREsults() could lead to OOME by using unbound list()
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/QueryImpl.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/QueryImpl.java 2008-08-14 18:36:46 UTC
(rev 15079)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/QueryImpl.java 2008-08-14 19:03:46 UTC
(rev 15080)
@@ -30,6 +30,7 @@
private org.hibernate.Query query;
private HibernateEntityManagerImplementor em;
private Boolean isPositional = null;
+ private int maxResults = -1;
public QueryImpl(org.hibernate.Query query, AbstractEntityManagerImpl em) {
this.query = query;
@@ -78,14 +79,16 @@
public Object getSingleResult() {
try {
+ if (maxResults != 1) query.setMaxResults( 2 ); //avoid OOME if the list is huge
List result = query.list();
+ if ( maxResults != -1 ) query.setMaxResults( maxResults ); //put back the original
value
if ( result.size() == 0 ) {
em.throwPersistenceException( new NoResultException( "No entity found for
query" ) );
}
else if ( result.size() > 1 ) {
Set uniqueResult = new HashSet(result);
if ( uniqueResult.size() > 1 ) {
- em.throwPersistenceException( new NonUniqueResultException( "result returns
" + uniqueResult.size() + " elements") );
+ em.throwPersistenceException( new NonUniqueResultException( "result returns
more than one elements") );
}
else {
return uniqueResult.iterator().next();
@@ -117,6 +120,7 @@
+ ") parameter passed in to setMaxResults"
);
}
+ this.maxResults = maxResult;
query.setMaxResults( maxResult );
return this;
}
Show replies by date