You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Sachin Deshpande (JIRA)" <ji...@apache.org> on 2008/10/07 14:11:44 UTC

[jira] Created: (OPENJPA-739) Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException

Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException
-------------------------------------------------------------------------------------------------------------------------

                 Key: OPENJPA-739
                 URL: https://issues.apache.org/jira/browse/OPENJPA-739
             Project: OpenJPA
          Issue Type: Bug
    Affects Versions: 1.1.0
         Environment: OS->Windows XP SP2  
App Server-> IBM Websphere App Server v6.1.0.13 
JDK-> IBM JDK v1.5 
JPA-> release 1.1.0
            Reporter: Sachin Deshpande
            Priority: Critical


Multi thread calls to EntityManagerFactory.getEntityManager() results in 
<openjpa-1.1.0-r422266:657916 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "*.*.*.entity.Rtlr.consCollection" declares "*.*.*.entity.Cons.rtlrCd" as its mapped-by field, but this field is not a direct relation.
	at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map(RelationToManyInverseKeyFieldStrategy.java:126)
	at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.map(RelationCollectionInverseKeyFieldStrategy.java:95)
	at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:120)
	at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
	at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:438)
	at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:403)
	at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:812)
	at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1625)
	at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:717)
	at org.apache.openjpa.meta.MetaDataRepository.resolveMapping(MetaDataRepository.java:677)
	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:555)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:308)
	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:248)
	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:219)
	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:187)
	at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:121)
	at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:310)
	at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:228)
	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:190)
	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
	at *.*.*.utility.DBUtilities.getEntityManager(DBUtilities.java:20)
       ...
       ...
       ...

      To access all EntityManagers we have the following custom class:

public class DBUtilities {

	private static EntityManagerFactory emf = null;
	
	static {
		CommonResources resource = CommonResources.getInstance();
		String env = resource.getValue("jndi.persistanceUnit.use");
		emf = Persistence.createEntityManagerFactory(env);
	}
	
	/**
	 * Return Entity Manager
	 * @return
	 */
	public static EntityManager getEntityManager() {
		return emf.createEntityManager();
	}
}

       The code is supposed to run in a multi-threaded environment where parallel threads invoke DBUtilities.getEntityManager() method. In such a scenario, we get the above exception.


      However, if we 'synchronize' the DBUtilities's getEntityManager() method, the exception does not occur and the code executes successfully. 

      This is contrary to our understanding that EntityManagerFactory is thread safe and EntityManagerFactory's .createEntityManager() method creates a new instance of EntityManager everytime though being invoked by multiple threads.

      Please advice.




-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-739) Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException

Posted by "Michael Dick (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-739?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637485#action_12637485 ] 

Michael Dick commented on OPENJPA-739:
--------------------------------------

Hi,

Do you have the WebSphere Feature Pack for EJB3 or are you using a "normal" version of WebSphere Application Server? 

The exception shown above comes from the automatic runtime enhancement feature of OpenJPA. Have you tried enhancing the entities manually with the PCEnhancer tool or PCEnhancer ant task? This might be another subclassing "gotcha" that needs to be documented. 

> Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-739
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-739
>             Project: OpenJPA
>          Issue Type: Bug
>    Affects Versions: 1.1.0
>         Environment: OS->Windows XP SP2  
> App Server-> IBM Websphere App Server v6.1.0.13 
> JDK-> IBM JDK v1.5 
> JPA-> release 1.1.0
>            Reporter: Sachin Deshpande
>            Priority: Critical
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Multi thread calls to EntityManagerFactory.getEntityManager() results in 
> <openjpa-1.1.0-r422266:657916 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "*.*.*.entity.Rtlr.consCollection" declares "*.*.*.entity.Cons.rtlrCd" as its mapped-by field, but this field is not a direct relation.
> 	at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map(RelationToManyInverseKeyFieldStrategy.java:126)
> 	at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.map(RelationCollectionInverseKeyFieldStrategy.java:95)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:120)
> 	at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:438)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:403)
> 	at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:812)
> 	at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1625)
> 	at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:717)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolveMapping(MetaDataRepository.java:677)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:555)
> 	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:308)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:248)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:219)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:187)
> 	at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:121)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:310)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:228)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:190)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
> 	at *.*.*.utility.DBUtilities.getEntityManager(DBUtilities.java:20)
>        ...
>        ...
>        ...
>       To access all EntityManagers we have the following custom class:
> public class DBUtilities {
> 	private static EntityManagerFactory emf = null;
> 	
> 	static {
> 		CommonResources resource = CommonResources.getInstance();
> 		String env = resource.getValue("jndi.persistanceUnit.use");
> 		emf = Persistence.createEntityManagerFactory(env);
> 	}
> 	
> 	/**
> 	 * Return Entity Manager
> 	 * @return
> 	 */
> 	public static EntityManager getEntityManager() {
> 		return emf.createEntityManager();
> 	}
> }
>        The code is supposed to run in a multi-threaded environment where parallel threads invoke DBUtilities.getEntityManager() method. In such a scenario, we get the above exception.
>       However, if we 'synchronize' the DBUtilities's getEntityManager() method, the exception does not occur and the code executes successfully. 
>       This is contrary to our understanding that EntityManagerFactory is thread safe and EntityManagerFactory's .createEntityManager() method creates a new instance of EntityManager everytime though being invoked by multiple threads.
>       Please advice.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-739) Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException

Posted by "Sachin Deshpande (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-739?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637783#action_12637783 ] 

Sachin Deshpande commented on OPENJPA-739:
------------------------------------------

Hi Michael,

We are using IBM Websphere App Server v6.1.0.13  with WebSphere Feature Pack for Webservices (v6.1.0.13). We have not installed WebSphere Feature Pack for EJB3.  We have not used PCEnhancer tool or PCEnhancer ant task.

We have generated the entity classes from RAD (Rational Application Developer) v 7.5. We have used these entity classes in our current DEV environment which uses Rational Software Architect (RSA ) v. 7.0.0.6. The JPA compile time and build time support is provided by  "web-jpa-dev.jar" 
that comes with Rational Application Developer v 7.5.

But we wonder how 'synchronizing' the DBUtilities's getEntityManager() method resolved the issue. Is EntityManagerFactory's .createEntityManager() thread safe?

Thanks,


> Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-739
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-739
>             Project: OpenJPA
>          Issue Type: Bug
>    Affects Versions: 1.1.0
>         Environment: OS->Windows XP SP2  
> App Server-> IBM Websphere App Server v6.1.0.13 
> JDK-> IBM JDK v1.5 
> JPA-> release 1.1.0
>            Reporter: Sachin Deshpande
>            Priority: Critical
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Multi thread calls to EntityManagerFactory.getEntityManager() results in 
> <openjpa-1.1.0-r422266:657916 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "*.*.*.entity.Rtlr.consCollection" declares "*.*.*.entity.Cons.rtlrCd" as its mapped-by field, but this field is not a direct relation.
> 	at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map(RelationToManyInverseKeyFieldStrategy.java:126)
> 	at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.map(RelationCollectionInverseKeyFieldStrategy.java:95)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:120)
> 	at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:438)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:403)
> 	at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:812)
> 	at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1625)
> 	at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:717)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolveMapping(MetaDataRepository.java:677)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:555)
> 	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:308)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:248)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:219)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:187)
> 	at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:121)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:310)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:228)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:190)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
> 	at *.*.*.utility.DBUtilities.getEntityManager(DBUtilities.java:20)
>        ...
>        ...
>        ...
>       To access all EntityManagers we have the following custom class:
> public class DBUtilities {
> 	private static EntityManagerFactory emf = null;
> 	
> 	static {
> 		CommonResources resource = CommonResources.getInstance();
> 		String env = resource.getValue("jndi.persistanceUnit.use");
> 		emf = Persistence.createEntityManagerFactory(env);
> 	}
> 	
> 	/**
> 	 * Return Entity Manager
> 	 * @return
> 	 */
> 	public static EntityManager getEntityManager() {
> 		return emf.createEntityManager();
> 	}
> }
>        The code is supposed to run in a multi-threaded environment where parallel threads invoke DBUtilities.getEntityManager() method. In such a scenario, we get the above exception.
>       However, if we 'synchronize' the DBUtilities's getEntityManager() method, the exception does not occur and the code executes successfully. 
>       This is contrary to our understanding that EntityManagerFactory is thread safe and EntityManagerFactory's .createEntityManager() method creates a new instance of EntityManager everytime though being invoked by multiple threads.
>       Please advice.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-739) Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException

Posted by "Michael Dick (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-739?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637937#action_12637937 ] 

Michael Dick commented on OPENJPA-739:
--------------------------------------

EntityManagerFactories are (or need to be) thread safe (EMs are not). It would be interesting to see if the problem only occurs when you use the subclassing support though. Sadly the subclassing support yields very different behavior from the "normal" enhancement path. So when we see it in the stack we tend to run the PCEnhancer first to eliminate it as a possibility.

The questions about the feature pack were just to make sure there were no conflicts with the version of OpenJPA that ships with the feature pack. 

I suspect that using build time enhancement will resolve the problem, but may not be permanent solution. The subclassing support is re-reading the metadata for your entities - it's this scan of the meta data that apparently isn't thread safe. Can you try using the PCEnhancer and seeing if that does resolve the issue? 

> Multi thread calls to EntityManagerFactory.getEntityManager() results in org.apache.openjpa.persistence.ArgumentException
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-739
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-739
>             Project: OpenJPA
>          Issue Type: Bug
>    Affects Versions: 1.1.0
>         Environment: OS->Windows XP SP2  
> App Server-> IBM Websphere App Server v6.1.0.13 
> JDK-> IBM JDK v1.5 
> JPA-> release 1.1.0
>            Reporter: Sachin Deshpande
>            Priority: Critical
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Multi thread calls to EntityManagerFactory.getEntityManager() results in 
> <openjpa-1.1.0-r422266:657916 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "*.*.*.entity.Rtlr.consCollection" declares "*.*.*.entity.Cons.rtlrCd" as its mapped-by field, but this field is not a direct relation.
> 	at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map(RelationToManyInverseKeyFieldStrategy.java:126)
> 	at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.map(RelationCollectionInverseKeyFieldStrategy.java:95)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:120)
> 	at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:438)
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:403)
> 	at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:812)
> 	at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1625)
> 	at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:717)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolveMapping(MetaDataRepository.java:677)
> 	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:555)
> 	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:308)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:248)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:219)
> 	at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:187)
> 	at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:121)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:310)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:228)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:190)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
> 	at *.*.*.utility.DBUtilities.getEntityManager(DBUtilities.java:20)
>        ...
>        ...
>        ...
>       To access all EntityManagers we have the following custom class:
> public class DBUtilities {
> 	private static EntityManagerFactory emf = null;
> 	
> 	static {
> 		CommonResources resource = CommonResources.getInstance();
> 		String env = resource.getValue("jndi.persistanceUnit.use");
> 		emf = Persistence.createEntityManagerFactory(env);
> 	}
> 	
> 	/**
> 	 * Return Entity Manager
> 	 * @return
> 	 */
> 	public static EntityManager getEntityManager() {
> 		return emf.createEntityManager();
> 	}
> }
>        The code is supposed to run in a multi-threaded environment where parallel threads invoke DBUtilities.getEntityManager() method. In such a scenario, we get the above exception.
>       However, if we 'synchronize' the DBUtilities's getEntityManager() method, the exception does not occur and the code executes successfully. 
>       This is contrary to our understanding that EntityManagerFactory is thread safe and EntityManagerFactory's .createEntityManager() method creates a new instance of EntityManager everytime though being invoked by multiple threads.
>       Please advice.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.