Обработка ресурсов JPA Entity Manager - PullRequest
1 голос
/ 06 мая 2010

Каждый раз, когда я вызываю метод JPA, его создание сущности и запрос привязки.

Мои постоянные свойства:

<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"/>

И я создаю менеджер сущностей, как показано ниже:

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

Есть ли хороший способ управления ресурсом менеджера сущностей, вместо этого создавать новые каждый раз, когда любое свойство можно установить в постоянном режиме. Помните, что это JPA.

См. Ниже журнал привязки каждый раз:

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

1 Ответ

1 голос
/ 06 мая 2010

Создание EntityManager не дорого и обычно выполняется для каждого запроса; однако создание EntityManagerFactory стоит очень дорого и должно выполняться только один раз.

В приложении Java SE это обычно делается в начале приложения. По сути, храните EMF в статической переменной в фабричном или вспомогательном классе. Только не забудьте emf.close() это в конце вашего заявления. См. этот пост или этот для некоторых примеров.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...