openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kevin Sutter" <kwsut...@gmail.com>
Subject Re: OpenJPA on standalone app
Date Wed, 24 Sep 2008 18:41:58 GMT
A few observations...

o  I would not recommend creating a static EntityManager instance.
EntityManagers are not thread safe (per the spec).  And, based on your game
idea, I would guess that eventually you will want multiple clients to access
this game.

o  The best way to use EntityManagers is via a get-use-close model.  So, I
would recommend a createEntityManager, use the EM, and then close it.

o  If you are looking to pool your EM's, then at a minimum, you will have to
call clear() before putting the EM back into the pool.

o  By default, an application-managed EM (like what you are using) will use
an extended persistence context.  This persistence context needs to be
maintained by your application.  This also means that you will need to call
clear() (or preferrably a close()) after a tran completes to detach these
entities from the persistence context.

Just a few ideas.  Good luck.
Kevin



On Wed, Sep 24, 2008 at 6:41 AM, Rodrigo Olmo Velasco <rolmovel@gmail.com>wrote:

> Hi all:
>
> I´m using openJPA 1.2.0 in a standalone application. Here is my
> persistence.xml
>
> [code]
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!--
>
> Licensed to the Apache Software Foundation (ASF) under one
>
> or more contributor license agreements. See the NOTICE file
>
> distributed with this work for additional information
>
> regarding copyright ownership. The ASF licenses this file
>
> to you under the Apache License, Version 2.0 (the
>
> "License"); you may not use this file except in compliance
>
> with the License. You may obtain a copy of the License at
>
> http://www.apache.org/licenses/LICENSE-2.0
>
> Unless required by applicable law or agreed to in writing,
>
> software distributed under the License is distributed on an
>
> "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
>
> KIND, either express or implied. See the License for the
>
> specific language governing permissions and limitations
>
> under the License.
>
> -->
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
> version="1.0">
>
> <!--
>
> We need to enumerate each persistent class first in the persistence.xml
>
> See: http://issues.apache.org/jira/browse/OPENJPA-78
>
> -->
>
> <!--
>
> A persistence unit is a set of listed persistent entities as well
>
> the configuration of an EntityManagerFactory. We configure each
>
> example in a separate persistence-unit.
>
> -->
>
> <persistence-unit name="XXX" transaction-type="RESOURCE_LOCAL">
>
> <!--
>
> The default provider can be OpenJPA, or some other product.
>
> This element is optional if OpenJPA is the only JPA provider
>
> in the current classloading environment, but can be specified
>
> in cases where there are multiple JPA implementations available.
>
> -->
>
> <provider>
>
> org.apache.openjpa.persistence.PersistenceProviderImpl
>
> </provider>
>
> <!-- We must enumerate each entity in the persistence unit -->
>
> <class>com.tecnalis.casino.blackjack.entity.Game</class>
>
> <class>com.tecnalis.casino.blackjack.entity.GameLog</class>
>
> <class>com.tecnalis.casino.blackjack.entity.Machine</class>
>
> <class>com.tecnalis.casino.blackjack.entity.MachineConfiguration</class>
>
> <properties>
>
> <!--
>
> We can configure the default OpenJPA properties here. They
>
> happen to be commented out here since the provided examples
>
> all specify the values via System properties.
>
> -->
>
> <property name="openjpa.ConnectionProperties"
>
> value="DriverClassName=com.mysql.jdbc.Driver,
>
> Url=jdbc:mysql://XXX?zeroDateTimeBehavior=convertToNull,
>
> MaxActive=100,
>
> MaxWait=10000,
>
> TestOnBorrow=true,
>
> Username=XXX,
>
> Password=XXX"/>
>
> <property name="openjpa.ConnectionDriverName"
>
> value="org.apache.commons.dbcp.BasicDataSource"/>
>
> <!--
>
> <property name="openjpa.ConnectionURL"
>
> value="jdbc:mysql://
> 192.168.16.6:3306/BLACKJACK?zeroDateTimeBehavior=convertToNull"/>
>
> <property name="openjpa.ConnectionDriverName"
>
> value="com.mysql.jdbc.Driver"/>
>
> <property name="openjpa.ConnectionUserName"
>
> value="root"/>
>
> <property name="openjpa.ConnectionPassword"
>
> value=""/>
>
> -->
>
> </properties>
>
> </persistence-unit>
>
> </persistence>
> [/code]
>
> And here is a class which uses domain classes
>
> [code]
> *
>
> public* *class* GameHelper {
>
> *private* *static* EntityManager *em*;
>
> *static* {
>
> EntityManagerFactory emf = Persistence.*createEntityManagerFactory*(
> "blackjack");
>
> *em* = emf.createEntityManager();
>
> }
>
> *public* *static* Game getGame(*int* idGame) *throws* Exception {
>
> *try* {
>
> *return* *em*.find(Game.*class*, idGame);
>
> } *catch* (Exception e) {
>
> e.printStackTrace();
>
> *throw* e;
>
> }
>
> }
>
> *public* *static* Game saveGame(*int* statusId, *int* playerId,
> *int*machineId, Date initDate, Date endDate)
> *throws* Exception {
>
> *try* {
>
> Game game = *new* Game();
>
> game.setStatusId(statusId);
>
> game.setPlayerId(playerId);
>
> game.setMachineId(*em*.find(Machine.*class*, machineId));
>
> game.setInitDate(*new* Timestamp(initDate.getTime()));
>
> game.setEndDate(*new* Timestamp(endDate.getTime()));
>
> *em*.getTransaction().begin();
>
> *em*.persist(game);
>
> *em*.getTransaction().commit();
>
> *return* game;
>
> } *catch* (Exception e) {
>
> e.printStackTrace();
>
> *throw* e;
>
> }
>
> }
>
> *public* *static* *void* saveTrace(*int* gameId, *int* methodId,
> *int*hands,
> *float* wager, *float* winning, String tableStatus) *throws* Exception {
>
> *try* {
>
> Game game = *em*.find(Game.*class*, gameId);
>
> GameLog gameLog = *new* GameLog();
>
> gameLog.setGameId(game);
>
> gameLog.setMethodId(methodId);
>
> gameLog.setHands(hands);
>
> gameLog.setWager(wager);
>
> gameLog.setWinning(winning);
>
> gameLog.setTableStatus(tableStatus);
>
> *em*.getTransaction().begin();
>
> *em*.persist(gameLog);
>
> *em*.getTransaction().commit();
>
> } *catch* (Exception e) {
>
> e.printStackTrace();
>
> *throw* e;
>
> }
>
> }
>
> }
> [/code]
>
>
> It works, but after stressing the GameHelper I realized that response time
> for updating data were too long.
> I tried clearing entity manager after commiting and response time increse a
> lot, perfect, but memory increase from 6M to 70M.
>
> Any idea of what is happening?
>
> Thanks
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message