views:

145

answers:

1

Every time I call JPA method its creating entity and binding query.

My persistence properties are:

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class"   value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

And I am creating entity manager the way shown below:

emf = Persistence.createEntityManagerFactory("pu");
em = emf.createEntityManager();
em = Persistence.createEntityManagerFactory("pu").createEntityManager();

Is there any nice way to manage entity manager resource instead create new every time or any property can set in persistence. Remember it's JPA.

See below binding log every time :

15:35:15,527 INFO  [AnnotationBinder] Binding entity from annotated class: *
15:35:15,527 INFO  [QueryBinder] Binding Named query: * => *
15:35:15,527 INFO  [QueryBinder] Binding Named query: * => *
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [QueryBinder] Binding Named query: 
15:35:15,527 INFO  [EntityBinder] Bind entity com.* on table *
15:35:15,542 INFO  [HibernateSearchEventListenerRegister] Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
15:35:15,542 INFO  [NamingHelper] JNDI InitialContext properties:{}
15:35:15,542 INFO  [DatasourceConnectionProvider] Using datasource: 
15:35:15,542 INFO  [SettingsFactory] RDBMS: 
and Real Application Testing options
15:35:15,542 INFO  [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 9.2.0.1.0
15:35:15,542 INFO  [Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
15:35:15,542 INFO  [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
15:35:15,542 INFO  [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recomm
ended)
15:35:15,542 INFO  [SettingsFactory] Automatic flush during beforeCompletion(): disabled
15:35:15,542 INFO  [SettingsFactory] Automatic session close at end of transaction: disabled
15:35:15,542 INFO  [SettingsFactory] JDBC batch size: 15
15:35:15,542 INFO  [SettingsFactory] JDBC batch updates for versioned data: disabled
15:35:15,542 INFO  [SettingsFactory] Scrollable result sets: enabled
15:35:15,542 INFO  [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
15:35:15,542 INFO  [SettingsFactory] Connection release mode: auto
15:35:15,542 INFO  [SettingsFactory] Default batch fetch size: 1
15:35:15,542 INFO  [SettingsFactory] Generate SQL with comments: disabled
15:35:15,542 INFO  [SettingsFactory] Order SQL updates by primary key: disabled
15:35:15,542 INFO  [SettingsFactory] Order SQL inserts for batching: disabled
15:35:15,542 INFO  [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
15:35:15,542 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
15:35:15,542 INFO  [SettingsFactory] Query language substitutions: {}
15:35:15,542 INFO  [SettingsFactory] JPA-QL strict compliance: enabled
15:35:15,542 INFO  [SettingsFactory] Second-level cache: enabled
15:35:15,542 INFO  [SettingsFactory] Query cache: enabled
15:35:15,542 INFO  [SettingsFactory] Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
15:35:15,542 INFO  [RegionFactoryCacheProviderBridge] Cache provider: net.sf.ehcache.hibernate.SingletonEhCacheProvider
15:35:15,542 INFO  [SettingsFactory] Optimize cache for minimal puts: disabled
15:35:15,542 INFO  [SettingsFactory] Structured second-level cache entries: disabled
15:35:15,542 INFO  [SettingsFactory] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory
15:35:15,542 INFO  [SettingsFactory] Statistics: disabled
15:35:15,542 INFO  [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
15:35:15,542 INFO  [SettingsFactory] Default entity-mode: pojo
15:35:15,542 INFO  [SettingsFactory] Named query checking : enabled
15:35:15,542 INFO  [SessionFactoryImpl] building session factory
15:35:15,542 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
15:35:15,542 INFO  [UpdateTimestampsCache] starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache
15:35:15,542 INFO  [StandardQueryCache] starting query cache at region: org.hibernate.cache.StandardQueryCache
A: 

Creating an EntityManager is not expensive and is typically done for each request; however, creating an EntityManagerFactory is very expensive and should be done only once.

In a Java SE application, this would typically be done at the start of the application. Basically, keep the EMF in a static variable in a factory or helper class. Just don't forget to emf.close() it at the end of your application. See this post or this one for some examples.

Pascal Thivent
Thanks for reply. I am using JPA in Java EE app. Its outside of container. Pure JPA. But going to use app very frequently and will have very heavy traffic so need to code and configure very efficiently to get high performance.Sorry create entity manager factory was typo. So How could I handle entity manager factory efficiently instead creating every time.