Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/897627/#msg_897627
I have Spring creating my EMF and injecting all properties information into PU. I use TomcatInstrumentationClassLoader in my webapp.

The first few queries that are executed right after starting up my application context invariably fail with

[0, 101] The query does not start with a valid identifier, has to be either SELECT, UPDATE or DELETE FROM.

The query in question is:

select distinct t from com.mydomain.myentity t where t.recstatus=0 and t.fileline=0

However, I have seen this happen on simple 'select t from myentity t' query as well.

The same query executes fine next time.

I can reproduce this error in unit tests ONLY if I run them, not debug. If I step through the test same query executes fine.

It almost seems to me this has got something to do with persistence environment initialization - like its not fully set up when I run my initial queries.

Peter
]]>Peter Zeltins2012-07-24T19:58:31-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/897946/#msg_897946
Best Regards,
Chris]]>Chris Delahunt2012-07-25T13:38:47-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/897980/#msg_897980

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [select distinct t from com.mydomain.models.myEntity t where t.recstatus=0 and t.fileline=0].
[0, 101] The query does not start with a valid identifier, has to be either SELECT, UPDATE or DELETE FROM.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1512)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1532)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy26.createQuery(Unknown Source)
at com.mydomain.TransactionsDAO.getTransactions(TransactionsDAO.java:169)
at com.mydomain.TransactionsDAO$$FastClassByCGLIB$$32cab5b7.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at com.mydomain.TransactionsDAO$$EnhancerByCGLIB$$32bc7e1c.getTransactions(<generated>)
at com.mydomain.TransactionStatusChecker.checkTransactionsStatus(TransactionStatusChecker.java:30)
at com.mydomain.TransactionStatusChecker$$FastClassByCGLIB$$5dbbb251.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at com.mydomain.TransactionStatusChecker$$EnhancerByCGLIB$$bf48da1a.checkTransactionsStatus(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:165)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select distinct t from com.mydomain.models.myEntity t where t.recstatus=0 and t.fileline=0].
[0, 101] The query does not start with a valid identifier, has to be either SELECT, UPDATE or DELETE FROM.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:150)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:325)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:270)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:157)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:138)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:112)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:98)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:82)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1510)
... 39 more

Peter
]]>Peter Zeltins2012-07-25T14:40:02-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/898015/#msg_898015
Peter]]>Peter Zeltins2012-07-25T15:39:44-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/898646/#msg_898646

Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
at java.util.HashMap$KeyIterator.next(HashMap.java:845)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1026)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.buildIdentifiers(JPQLQueryBNF.java:175)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.populateIdentifiers(JPQLQueryBNF.java:449)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.getExpressionFactory(JPQLQueryBNF.java:260)
at org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.parseSingleExpression(AbstractExpression.java:1003)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.parse(JPQLExpression.java:307)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.<init>(JPQLExpression.java:129)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.<init>(JPQLExpression.java:91)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:258)

Weird. I wonder if this problem is Spring-related? ]]>Peter Zeltins2012-07-27T04:02:12-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/899340/#msg_899340

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Internal problem encountered while compiling [select distinct t from myentity t where t.recstatus=0 and t.fileline=0].
Internal Exception: java.lang.UnsupportedOperationException
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1512)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1532)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy27.createQuery(Unknown Source)
at TransactionsDAO.getTransactions(TransactionsDAO.java:169)
at TransactionsDAO$$FastClassByCGLIB$$32cab5b7.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at TransactionsDAO$$EnhancerByCGLIB$$bc5acbc4.getTransactions(<generated>)
at TransactionStatusChecker.checkTransactionsStatus(TransactionStatusChecker.java:30)
at TransactionStatusChecker$$FastClassByCGLIB$$5dbbb251.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at TransactionStatusChecker$$EnhancerByCGLIB$$48e727c2.checkTransactionsStatus(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:165)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Internal problem encountered while compiling [select distinct t from myentity t where t.recstatus=0 and t.fileline=0].
Internal Exception: java.lang.UnsupportedOperationException
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildUnexpectedException(HermesParser.java:200)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:288)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:157)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:138)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:112)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:98)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:82)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1510)
... 39 more
Caused by: java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.addAll(Collections.java:1044)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.getExpressionFactoryIds(JPQLQueryBNF.java:280)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.buildIdentifiers(JPQLQueryBNF.java:175)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.populateIdentifiers(JPQLQueryBNF.java:449)
at org.eclipse.persistence.jpa.jpql.parser.JPQLQueryBNF.getExpressionFactory(JPQLQueryBNF.java:260)
at org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.parseSingleExpression(AbstractExpression.java:1003)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.parse(JPQLExpression.java:307)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.<init>(JPQLExpression.java:129)
at org.eclipse.persistence.jpa.jpql.parser.JPQLExpression.<init>(JPQLExpression.java:91)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:258)
... 45 more
]]>Peter Zeltins2012-07-31T14:00:57-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/899375/#msg_899375
It is apparent to me that EclipseLink internal persistence unit setup is not thread-safe. This problem only shows up if you run multiple queries at the same time right after setting up the PU.

All in all this seems like a serious oversight to me, and IMO should be fixed. I'll see if I can come up with a reliable test case and submit a bug for it.

Peter
]]>Peter Zeltins2012-07-31T15:22:14-00:00Re: Error on first queryhttps://www.eclipse.org/forums/index.php/mv/msg/368031/905614/#msg_905614
Also, for anyone else who's not using Spring, I came up with a more portable workaround. Create a class with the below code in it and add it to your persistence unit's properties using the "eclipselink.session-event-listener" property.

import org.eclipse.persistence.exceptions.JPQLException;
import org.eclipse.persistence.internal.jpa.JPAQuery;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;
public class RaceConditionWorkaround extends SessionEventAdapter {
@Override
public void postLogin(SessionEvent event) {
/*
* This is a nasty hack to work around a race condition in EclipseLink. If you try to
* execute two queries at the same time while a persistence unit is starting up, you
* can get bogus exceptions about syntax errors. Executing this fake query causes the
* query parser to be initialized before any other queries can execute.
*
* For more details, see:
* http://www.eclipse.org/forums/index.php/t/368031/
*/
try {
event.getSession().executeQuery(new JPAQuery("SELECT x FROM Fake x"));
} catch (JPQLException e) {} // deliberately suppress the exception, we expect it to happen
}
}