Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Error on first query
Error on first query [message #897627] Tue, 24 July 2012 19:58 Go to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
EL 2.4, Spring 3.1, Mysql 5, Tomcat 6, EL is set up for static weaving.

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.

Here is my Spring ORM context config:

<context:annotation-config />
	
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" mode="proxy"/>

<bean id="dataConfigPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
     	<property name="location" value="WEB-INF/config/${build_environment}.properties" />   
   </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		p:driverClassName="com.mysql.jdbc.Driver" p:url="${hibernate.db_uri}" p:username="${hibernate.db_user}" p:password="${hibernate.db_password}"
/>
		
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    	<property name="defaultDataSource" ref="dataSource"></property>
    </bean>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
		p:persistenceUnitManager-ref="persistenceUnitManager"
		p:persistenceUnitName="MyPersistenceUnit" p:jpaVendorAdapter-ref="jpaAdapter"  >
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
		p:entityManagerFactory-ref="entityManagerFactory" />

    <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" p:showSql="true"/>



and here is persistence.xml:


	<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
		<class>com.mydomain.myentity</class>
				<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<validation-mode>NONE</validation-mode>
		<properties>
			<property name="eclipselink.weaving" value="static"/>
		</properties>
	</persistence-unit>
</persistence>


Any hints on how to fix this would be appreciated!

Peter
Re: Error on first query [message #897946 is a reply to message #897627] Wed, 25 July 2012 13:38 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1023
Registered: July 2009
Senior Member
Can you show how you are creating the query and the full exception stack trace? If the error is occuring because creating the query is the first thing that happens in the EntityManager, it could be due to lazy initilization of the persistence unit causing JPQL named queries to be loaded at the same time.

Best Regards,
Chris
Re: Error on first query [message #897980 is a reply to message #897946] Wed, 25 July 2012 14:40 Go to previous messageGo to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
Here is full stack trace:


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


And this is excerpt from code:

@PersistenceContext(type=PersistenceContextType.TRANSACTION,unitName="myPersistenceUnit")
protected EntityManager em;

TypedQuery<MyEntity> _query = em.createQuery(_sql, MyEntity.class);
if ( filterArgs != null && filterArgs.length > 0 ) {
for ( int i=0; i<filterArgs.length; i++) {
	_query.setParameter(i+1, filterArgs[i]);
}
}
		
Collection<MyEntity> _retval = _query.getResultList();


Thanks for looking into this!

Peter
Re: Error on first query [message #898015 is a reply to message #897946] Wed, 25 July 2012 15:39 Go to previous messageGo to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
I just noticed the same query fails about once a day afterwards. It is being run every minute by scheduler, and almost always executes fine.

Peter
Re: Error on first query [message #898646 is a reply to message #898015] Fri, 27 July 2012 04:02 Go to previous messageGo to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
Here, I have one more interesting exception thrown at startup of Tomcat container:


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?

[Updated on: Fri, 27 July 2012 14:25]

Report message to a moderator

Re: Error on first query [message #899340 is a reply to message #898646] Tue, 31 July 2012 14:00 Go to previous messageGo to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
More weird errors:


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
Re: Error on first query [message #899375 is a reply to message #899340] Tue, 31 July 2012 15:22 Go to previous messageGo to next message
Peter Zeltins is currently offline Peter Zeltins
Messages: 8
Registered: July 2012
Junior Member
I think I have it figured out.

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.

A workaround is to execute dummy query first. Here is my EMF Spring bean:


import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

public class EclipseLinkEntityManagerFactory extends LocalContainerEntityManagerFactoryBean {

	public void initializePersistenceUnit() {
		super.getObject().createEntityManager().createQuery("select t from " + MyEntity.class.getName() + " t where t.id=0");
	}
	
}


and the bean definition:


<bean id="entityManagerFactory" class="com.mydomain.eclipselink.EclipseLinkEntityManagerFactory"
		p:persistenceUnitManager-ref="persistenceUnitManager"
		p:persistenceUnitName="MyPU" p:jpaVendorAdapter-ref="jpaAdapter" 
		init-method="initializePersistenceUnit" >
</bean>


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
Re: Error on first query [message #905614 is a reply to message #897627] Thu, 30 August 2012 18:47 Go to previous messageGo to next message
Rich DiCroce is currently offline Rich DiCroce
Messages: 5
Registered: August 2012
Junior Member
Peter, did you end up filing a bug report for this? It happens consistently for me on JBoss AS 7.1.1.Final with a pair of @Singleton @Startup EJBs that both need to query the database.

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
	}

}
Re: Error on first query [message #908503 is a reply to message #905614] Wed, 05 September 2012 13:38 Go to previous message
Rich DiCroce is currently offline Rich DiCroce
Messages: 5
Registered: August 2012
Junior Member
I have now filed a bug report for this issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=388852
Previous Topic:Implementing post merge events
Next Topic:jboss tools hibernate is getting error while installing @ eclipse indigo sr2
Goto Forum:
  


Current Time: Sun Sep 21 06:15:10 GMT 2014

Powered by FUDForum. Page generated in 0.04439 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software