• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to use datasource for Hibernate Entity Manager

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to create a datasource and need to use that every time.

Right now I am giving these properties in persistance.xml.

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="pramod" />
<property name="hibernate.connection.password" value="pramod" />
<property name="hibernate.connection.url" value="jdbc racle:thin:@localhost:1521:localdb" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>


and when ever I need to persist or remove I am doing this each and every time
EntityManagerFactory emf =Persistence.createEntityManagerFactory("usermgt");

Give me some sugessions so that I can use this commonly. I am using weblogic9.2.

Thanks,
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many solutions out there showing how to make your EntityManagerFactory into a Singleton, so you only create one instance of an EntityManagerFactory.

Mark
 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to read the database properties from a configurable properties file and set to the persistance.xml how can I do this for example if I need to change the database I need to go to persistance.xml and need to change the values, but I need like there should be a properties file in that I need to configure all the database properties and need to access those values to persistance.xml how can I do this?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole porperties and persistence.xml can all be done through code too, if you really want to have flexibility like that without having to rebuild each time.

Mark
 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting exception while using jta-data-source

I am using Weblogic 9.2, I have created a DataSource with the name UserDs with all the properties in Weblogic and I gave that datasource name in jta-data-source I am getting the exception as shown below.

<persistence>
<persistence-unit name="userconsole">
<jta-data-source>UserDs</jta-data-source>
<class>user.Users</class>

<properties>

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>


javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not find datasource
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:217)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
at entity.PersonTest.initEmfAndEm(PersonTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)



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

1937 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
1953 [main] FATAL org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: OamDs
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)


------------------
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like these two lines make the difference

1937 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
1953 [main] FATAL org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: OamDs
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an

So either you need to include a jndi.properties file in your app, or the app server needs one.

Mark
 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created jndi.properties file in class path and I have given the properties in it.

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:7001

and in the code I have given as below but still I am getting the same exception. Do I need to code any thing? My datasource name is UserDs



-----------------------------------------
1718 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
8249 [main] FATAL org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: OamDs
javax.naming.NameNotFoundException: Unable to resolve 'UserDs'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'UserDs'. Resolved '']; remaining name 'UserDs'
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:195)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:338)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:252)
at weblogic.jndi.internal.ServerNamingNode_921_WLStub.lookup(Unknown Source)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:374)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:362)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how Weblogic 9.2 does this.

In JBoss, you actually don't need to create your own jndi.properties, there is already one in the app server there. You just deploy your datasource with an xxx-ds.xml file, and in your hibernate or JPA config file pointing to that datasource.

You might want to create a new thread in the Weblogic forum for this issue. Unless someone else comes in this thread with Weblogic 9.2 experience.

Good Luck

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic