You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Janap <ja...@gmail.com> on 2007/07/13 16:30:05 UTC

Configure EntityManager with dynamic connection

Hello, 

I want to know if it is possible to create an instance of EntityManager
using the connection properties(i.e not only Username and Pwd but also
ConnectionURL and jdbc driver).

In other words, I want to use EntityManager for each user session and close
it when the user logs out. I know this is out of place but I dont want to
implement any sort of pooling.

The ideal architecture would be to create an instance of
EntityManagerFactory(when Tomcat starts) in application scope. Further I
want to use this factory to create an EntityManager when a user logs in.

Any ideas if it is possible or not? 

I implemented a simple DataSource interface which returns a
java.sql.Connection object.

In my persistence.xml this code works :

<property name="openjpa.ConnectionDriverName" value="util.myDataSource"/>
<property name="openjpa.ConnectionFactoryProperties"
value="PrettyPrint=true,PrettyPrintLineLength=80"/  >    
<property name="openjpa.Log" value="DefaultLevel=TRACE,Tool=TRACE"/>



EntityManagerFactory emf =
Persistence.createEntityManagerFactory("JPATest");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

Now I want to port the code into my classes :

EntityManagerFactory emf = Persistence.createEntityManagerFactory(null);
Map myProperties = new HashMap();
myProperties.put("openjpa.ConnectionDriverName","util.myDataSource");
myProperties.put("openjpa.ConnectionFactoryProperties","PrettyPrint=true,
PrettyPrintLineLength=80");
EntityManager em = emf.createEntityManager(myProperties);
em.getTransaction().begin();

But it throws the following excpetion :  

Exception in thread "main" <4|true|0.9.6-incubating>
org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or
DataSource class name must be specified in the ConnectionDriverName
property.


thx,
Jan



-- 
View this message in context: http://www.nabble.com/Configure-EntityManager-with-dynamic-connection-tf4074808.html#a11580724
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Configure EntityManager with dynamic connection

Posted by Janap <ja...@gmail.com>.
Yes my next step will be to try with 0.9.7 libs. 
Also, I have removed properties from persistence.xml. It now has only mapped
classes and persistence provider info.

No, i have not registered my datasource implementation with JNDI,will try it
out too..here is the stack trace

-------------------------------------------------------
0  TRACE  [main] openjpa.Runtime - Setting the following properties from
"file:/C:/Programme/Eclipse/workspace/JPATest/build/classes/META-INF/persistence.xml"
into configuration: {openjpa.BrokerFactory=jdbc,
openjpa.Log=DefaultLevel=TRACE,Tool=TRACE,
openjpa.MetaDataFactory=Types=EntityBeans.Scale,
javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl}
62  INFO   [main] openjpa.Runtime - Starting OpenJPA 0.9.6-incubating
62  TRACE  [main] openjpa.Runtime - Properties:
openjpa.EntityManagerFactory: default
openjpa.DataCache: false
openjpa.MetaDataFactory: Types=EntityBeans.Scale
openjpa.InverseManager: false
openjpa.ReadLockLevel: read
openjpa.DataCacheManager: default
openjpa.jdbc.SubclassFetchMode: join
openjpa.jdbc.UpdateManager: default
openjpa.jdbc.SynchronizeMappings: false
openjpa.NontransactionalRead: true
openjpa.QueryCompilationCache: true
openjpa.MaxFetchDepth: -1
openjpa.RetainState: true
openjpa.DynamicDataStructs: false
openjpa.BrokerFactory: jdbc
openjpa.WriteLockLevel: write
openjpa.ManagedRuntime: auto
openjpa.jdbc.EagerFetchMode: parallel
openjpa.RestoreState: immutable
openjpa.jdbc.SchemaFactory: dynamic
openjpa.LockManager: version
openjpa.BrokerImpl: default
openjpa.NontransactionalWrite: true
openjpa.MetaDataRepository: default
openjpa.Log: DefaultLevel=TRACE,Tool=TRACE
openjpa.jdbc.ResultSetType: forward-only
openjpa.AutoDetach: 
openjpa.ConnectionRetainMode: on-demand
openjpa.SavepointManager: in-mem
openjpa.Optimistic: true
openjpa.ConnectionFactoryMode: local
openjpa.Sequence: table
openjpa.FetchGroups: default
openjpa.jdbc.Schemas: 
openjpa.OrphanedKeyAction: log
openjpa.FlushBeforeQueries: true
openjpa.AutoClear: datastore
openjpa.Compatibility: default
openjpa.DetachState: loaded
openjpa.jdbc.LRSSize: query
openjpa.Multithreaded: false
openjpa.FetchBatchSize: -1
openjpa.jdbc.SQLFactory: default
openjpa.IgnoreChanges: false
javax.persistence.provider:
org.apache.openjpa.persistence.PersistenceProviderImpl
openjpa.jdbc.MappingDefaults: jpa
openjpa.TransactionMode: local
openjpa.RetryClassRegistration: false
openjpa.jdbc.FetchDirection: forward
openjpa.ClassResolver: default
openjpa.LockTimeout: -1
openjpa.DataCacheTimeout: -1
openjpa.QueryCache: true
openjpa.jdbc.DriverDataSource: simple
openjpa.jdbc.TransactionIsolation: default
openjpa.ProxyManager: default
156  TRACE  [main] openjpa.MetaData - Using metadata factory
"org.apache.openjpa.persistence.jdbc.PersistenceMappingFactory@134bed0".
171  INFO   [main] openjpa.jdbc.JDBC - OpenJPA will now connect to the
database to attempt to determine what type of database dictionary to use. 
To prevent this connection in the future, set your openjpa.jdbc.DBDictionary
configuration property to the appropriate value for your database (see the
documentation for available values).
Exception in thread "main" <4|true|0.9.6-incubating>
org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or
DataSource class name must be specified in the ConnectionDriverName
property.
	at
org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:67)
	at
org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:797)
	at
org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:563)
	at
org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1167)
	at
org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:355)
	at
org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:280)
	at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:99)
	at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:70)
	at
org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:829)
	at
org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:824)
	at
org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:526)
	at
org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:147)
	at
org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:139)
	at
org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:187)
	at
org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:52)


----------------------------------------------------------


thx for the tip,
have a nice weekend
Jan


Patrick Linskey-2 wrote:
> 
> Hi,
> 
> I think that this might be a bug in 0.9.6 that has since been
> resolved, although I'm not 100% sure. Note that 0.9.6 is pretty old at
> this point.
> 
> Also, you shouldn't need to specify the properties both in the
> persistence.xml and when you obtain the EMF -- just one spot is good
> enough.
> 
> Are you registering your DataSource implementation in JNDI? Another
> possible approach is to just specify the full classname of your
> DataSource implementation, and OpenJPA will instantiate it. For
> details about this, see:
> http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_dbsetup_thirdparty
> 
> Can you send the full stack trace also?
> 
> -Patrick
> 
> On 7/13/07, Janap <ja...@gmail.com> wrote:
>>
>> Hello,
>>
>> I want to know if it is possible to create an instance of EntityManager
>> using the connection properties(i.e not only Username and Pwd but also
>> ConnectionURL and jdbc driver).
>>
>> In other words, I want to use EntityManager for each user session and
>> close
>> it when the user logs out. I know this is out of place but I dont want to
>> implement any sort of pooling.
>>
>> The ideal architecture would be to create an instance of
>> EntityManagerFactory(when Tomcat starts) in application scope. Further I
>> want to use this factory to create an EntityManager when a user logs in.
>>
>> Any ideas if it is possible or not?
>>
>> I implemented a simple DataSource interface which returns a
>> java.sql.Connection object.
>>
>> In my persistence.xml this code works :
>>
>> <property name="openjpa.ConnectionDriverName" value="util.myDataSource"/>
>> <property name="openjpa.ConnectionFactoryProperties"
>> value="PrettyPrint=true,PrettyPrintLineLength=80"/  >
>> <property name="openjpa.Log" value="DefaultLevel=TRACE,Tool=TRACE"/>
>>
>>
>>
>> EntityManagerFactory emf =
>> Persistence.createEntityManagerFactory("JPATest");
>> EntityManager em = emf.createEntityManager();
>> em.getTransaction().begin();
>>
>> Now I want to port the code into my classes :
>>
>> EntityManagerFactory emf = Persistence.createEntityManagerFactory(null);
>> Map myProperties = new HashMap();
>> myProperties.put("openjpa.ConnectionDriverName","util.myDataSource");
>> myProperties.put("openjpa.ConnectionFactoryProperties","PrettyPrint=true,
>> PrettyPrintLineLength=80");
>> EntityManager em = emf.createEntityManager(myProperties);
>> em.getTransaction().begin();
>>
>> But it throws the following excpetion :
>>
>> Exception in thread "main" <4|true|0.9.6-incubating>
>> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or
>> DataSource class name must be specified in the ConnectionDriverName
>> property.
>>
>>
>> thx,
>> Jan
>>
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Configure-EntityManager-with-dynamic-connection-tf4074808.html#a11580724
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
>>
> 
> 
> -- 
> Patrick Linskey
> 202 669 5907
> 
> 

-- 
View this message in context: http://www.nabble.com/Configure-EntityManager-with-dynamic-connection-tf4074808.html#a11581969
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Configure EntityManager with dynamic connection

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

I think that this might be a bug in 0.9.6 that has since been
resolved, although I'm not 100% sure. Note that 0.9.6 is pretty old at
this point.

Also, you shouldn't need to specify the properties both in the
persistence.xml and when you obtain the EMF -- just one spot is good
enough.

Are you registering your DataSource implementation in JNDI? Another
possible approach is to just specify the full classname of your
DataSource implementation, and OpenJPA will instantiate it. For
details about this, see:
http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_dbsetup_thirdparty

Can you send the full stack trace also?

-Patrick

On 7/13/07, Janap <ja...@gmail.com> wrote:
>
> Hello,
>
> I want to know if it is possible to create an instance of EntityManager
> using the connection properties(i.e not only Username and Pwd but also
> ConnectionURL and jdbc driver).
>
> In other words, I want to use EntityManager for each user session and close
> it when the user logs out. I know this is out of place but I dont want to
> implement any sort of pooling.
>
> The ideal architecture would be to create an instance of
> EntityManagerFactory(when Tomcat starts) in application scope. Further I
> want to use this factory to create an EntityManager when a user logs in.
>
> Any ideas if it is possible or not?
>
> I implemented a simple DataSource interface which returns a
> java.sql.Connection object.
>
> In my persistence.xml this code works :
>
> <property name="openjpa.ConnectionDriverName" value="util.myDataSource"/>
> <property name="openjpa.ConnectionFactoryProperties"
> value="PrettyPrint=true,PrettyPrintLineLength=80"/  >
> <property name="openjpa.Log" value="DefaultLevel=TRACE,Tool=TRACE"/>
>
>
>
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("JPATest");
> EntityManager em = emf.createEntityManager();
> em.getTransaction().begin();
>
> Now I want to port the code into my classes :
>
> EntityManagerFactory emf = Persistence.createEntityManagerFactory(null);
> Map myProperties = new HashMap();
> myProperties.put("openjpa.ConnectionDriverName","util.myDataSource");
> myProperties.put("openjpa.ConnectionFactoryProperties","PrettyPrint=true,
> PrettyPrintLineLength=80");
> EntityManager em = emf.createEntityManager(myProperties);
> em.getTransaction().begin();
>
> But it throws the following excpetion :
>
> Exception in thread "main" <4|true|0.9.6-incubating>
> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or
> DataSource class name must be specified in the ConnectionDriverName
> property.
>
>
> thx,
> Jan
>
>
>
> --
> View this message in context: http://www.nabble.com/Configure-EntityManager-with-dynamic-connection-tf4074808.html#a11580724
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>


-- 
Patrick Linskey
202 669 5907