geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r901896 - in /geronimo/daytrader/trunk/modules: core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/ entities/src/main/resources/META-INF/
Date Thu, 21 Jan 2010 22:29:06 GMT
Author: dwoods
Date: Thu Jan 21 22:29:06 2010
New Revision: 901896

URL: http://svn.apache.org/viewvc?rev=901896&view=rev
Log:
merged in a few updates from Joe's sandbox and a few em.close() updates of my own.  Still
not working on Tomcat 6.0.20.

Modified:
    geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJDBCDirect.java
    geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJPADirect.java
    geronimo/daytrader/trunk/modules/entities/src/main/resources/META-INF/persistence.xml

Modified: geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJDBCDirect.java
URL: http://svn.apache.org/viewvc/geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJDBCDirect.java?rev=901896&r1=901895&r2=901896&view=diff
==============================================================================
--- geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJDBCDirect.java
(original)
+++ geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJDBCDirect.java
Thu Jan 21 22:29:06 2010
@@ -66,6 +66,8 @@
 
     private boolean inSession = false;
 
+    private static InitialContext context;
+
     /**
      * Zero arg constructor for TradeJDBCDirect
      */
@@ -162,7 +164,7 @@
         }
 
         catch (Exception e) {
-            Log.error("TradeJDBCDirect:login -- error logging in user", e);
+            Log.error("TradeJDBCDirect:getMarketSummary -- error getting summary", e);
             rollBack(conn, e);
         } finally {
             releaseConn(conn);
@@ -1253,9 +1255,14 @@
      * @see TradeServices#logout(String)
      */
     public void logout(String userID) throws Exception {
+
+        Connection conn = null;
+
+        if (Log.doActionTrace())
+            Log.trace("TradeAction:logout", userID);
+
         if (Log.doTrace())
             Log.trace("TradeJDBCDirect:logout - inSession(" + this.inSession + ")", userID);
-        Connection conn = null;
         try {
             conn = getConn();
             PreparedStatement stmt = getStatement(conn, logoutSQL);
@@ -1277,7 +1284,7 @@
      */
 
     public AccountDataBean register(String userID, String password, String fullname, String
address, String email,
-        String creditcard, BigDecimal openBalance) throws Exception {
+        String creditCard, BigDecimal openBalance) throws Exception {
 
         AccountDataBean accountData = null;
         Connection conn = null;
@@ -1312,7 +1319,7 @@
             stmt.setString(3, fullname);
             stmt.setString(4, address);
             stmt.setString(5, email);
-            stmt.setString(6, creditcard);
+            stmt.setString(6, creditCard);
             stmt.executeUpdate();
             stmt.close();
 
@@ -1890,8 +1897,6 @@
         }
     }
 
-    private static InitialContext context;
-
     /**
      * Gets the inGlobalTxn
      * 
@@ -1911,4 +1916,13 @@
         this.inGlobalTxn = inGlobalTxn;
     }
 
+    /**
+     * Get mode - returns the persistence mode (TradeConfig.JDBC)
+     * 
+     * @return int mode
+     */
+    public int getMode() {
+        return TradeConfig.JDBC;
+    }
+
 }

Modified: geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJPADirect.java
URL: http://svn.apache.org/viewvc/geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJPADirect.java?rev=901896&r1=901895&r2=901896&view=diff
==============================================================================
--- geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJPADirect.java
(original)
+++ geronimo/daytrader/trunk/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/direct/TradeJPADirect.java
Thu Jan 21 22:29:06 2010
@@ -25,7 +25,6 @@
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
-import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceUnit;
 import javax.persistence.Query;
 
@@ -56,9 +55,10 @@
  * @see org.apache.geronimo.samples.daytrader.TradeServices
  * 
  */
+
 public class TradeJPADirect implements TradeServices, TradeDBServices {
 
-    @PersistenceUnit(name="daytrader")
+    @PersistenceUnit(unitName="daytrader")
     private static EntityManagerFactory emf;
 
     private static BigDecimal ZERO = new BigDecimal(0.0);
@@ -71,9 +71,10 @@
     public TradeJPADirect() {
 
 
-        // TO-DO why
+        // FIXME - Why is this here???
         TradeConfig.setPublishQuotePriceChange(false);
         if (emf == null) {
+            Log.error("TradeJPADirect.ctor:  Calling createEntityManagerFactory()");
             // creating entity manager factory. the persistence xml must be
             // place under src/META-INF/
             emf = Persistence.createEntityManagerFactory("daytrader");
@@ -83,14 +84,16 @@
             init();
     }
 
+    public void setEmf (EntityManagerFactory em) { 
+        emf = em;
+    }
+
     public static synchronized void init() {
         if (initialized)
             return;
         if (Log.doTrace())
             Log.trace("TradeJPADirect:init -- *** initializing");
 
-        // TO-DO-for some reason this is not setting it to false- added
-        // to the construct.
         TradeConfig.setPublishQuotePriceChange(false);
 
         if (Log.doTrace())
@@ -165,17 +168,14 @@
         catch (Exception e) {
             Log.error("TradeJPADirect:getMarketSummary", e);
             throw new RuntimeException("TradeJPADirect:getMarketSummary -- error ", e);
+        } finally {
+            entityManager.close();
         }
-        /*
-         * closing entitymanager
-         */
-        entityManager.close();
 
         return marketSummaryData;
     }
 
-    public OrderDataBean buy(String userID, String symbol, double quantity,
-                             int orderProcessingMode) {
+    public OrderDataBean buy(String userID, String symbol, double quantity, int orderProcessingMode)
{
         OrderDataBean order = null;
         BigDecimal total;
         /*
@@ -198,8 +198,7 @@
 
             HoldingDataBean holding = null; // The holding will be created by this buy order
 
-            order = createOrder(account, quote, holding, "buy", quantity,
-                                entityManager);
+            order = createOrder(account, quote, holding, "buy", quantity, entityManager);
 
             // order = createOrder(account, quote, holding, "buy", quantity);
             // UPDATE - account should be credited during completeOrder
@@ -229,10 +228,12 @@
 
             // throw new EJBException(e);
             throw new RuntimeException(e);
-        }
-        if (entityManager != null) {
-            entityManager.close();
-            entityManager = null;
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
+
         }
 
         // after the purchase or sell of a stock, update the stocks volume and
@@ -269,12 +270,6 @@
                 orderData.setOrderStatus("cancelled");
 
                 entityManager.persist(orderData);
-
-                if (entityManager != null) {
-                    entityManager.close();
-                    entityManager = null;
-
-                }
                 entityManager.getTransaction().commit();
                 return orderData;
             }
@@ -315,11 +310,11 @@
             entityManager.getTransaction().rollback();
 
             throw new RuntimeException("TradeJPADirect:sell(" + userID + "," + holdingID
+ ")", e);
-        }
-
-        if (entityManager != null) {
-            entityManager.close();
-            entityManager = null;
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
         }
 
         if (!(order.getOrderStatus().equalsIgnoreCase("cancelled")))
@@ -419,11 +414,11 @@
         catch (Exception e) {
             e.printStackTrace();
             entityManager.getTransaction().rollback();
-        }
-
-        if (entityManager != null) {
-            entityManager.close();
-            entityManager = null;
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
         }
 
         return order;
@@ -446,10 +441,9 @@
         }
         catch (Exception e) {
             entityManager.getTransaction().rollback();
+        } finally {
             entityManager.close();
-            entityManager = null;
         }
-        entityManager.close();
     }
 
     public void orderCompleted(String userID, Integer orderID) {
@@ -558,6 +552,11 @@
             throw new RuntimeException(
                                       "TradeJPADirect.getClosedOrders - error", e);
 
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
         }
 
     }
@@ -590,12 +589,16 @@
         }
         catch (Exception e) {
             Log.error("TradeJPADirect:createQuote -- exception creating Quote", e);
-            System.out
-            .println("TradeJPADirect:createQuote -- exception creating Quote");
             entityManager.close();
             entityManager = null;
             throw new RuntimeException(e);
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
         }
+
     }
 
     public QuoteDataBean getQuote(String symbol) {
@@ -641,18 +644,15 @@
          * "quoteejb.quoteForUpdate" defined in QuoteDatabean
          */
         EntityManager entityManager = emf.createEntityManager();
-        QuoteDataBean quote = new QuoteDataBean();
+        QuoteDataBean quote = null;
         if (TradeConfig.jpaLayer == TradeConfig.HIBERNATE) {
             quote = entityManager.find(QuoteDataBean.class, symbol);
-        }
-
-        if (TradeConfig.jpaLayer == TradeConfig.OPENJPA) {
+        } else if (TradeConfig.jpaLayer == TradeConfig.OPENJPA) {
   
             Query q = entityManager.createNamedQuery("quoteejb.quoteForUpdate");
             q.setParameter(1, symbol);
   
             quote = (QuoteDataBean) q.getSingleResult();
-
         }
 
         BigDecimal oldPrice = quote.getPrice();
@@ -679,11 +679,11 @@
         }
         catch (Exception e) {
             entityManager.getTransaction().rollback();
-        }
-
-        if (entityManager != null) {
-            entityManager.close();
-            entityManager = null;
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+                entityManager = null;
+            }
         }
 
         this.publishQuotePriceChange(quote, oldPrice, changeFactor, sharesTraded);
@@ -721,8 +721,11 @@
     public HoldingDataBean getHolding(Integer holdingID) {
         if (Log.doTrace())
             Log.trace("TradeJPADirect:getHolding", holdingID);
+        HoldingDataBean holding;
         EntityManager entityManager = emf.createEntityManager();
-        return entityManager.find(HoldingDataBean.class, holdingID);
+        holding = entityManager.find(HoldingDataBean.class, holdingID);
+        entityManager.close();
+        return holding;
     }
 
     public AccountDataBean getAccountData(String userID) {
@@ -791,12 +794,11 @@
             entityManager.getTransaction().begin();
             entityManager.merge(temp);
             entityManager.getTransaction().commit();
-            entityManager.close();
         }
         catch (Exception e) {
             entityManager.getTransaction().rollback();
+        } finally {
             entityManager.close();
-            entityManager = null;
         }
 
         return temp;
@@ -846,12 +848,11 @@
             entityManager.getTransaction().begin();
             account.logout();
             entityManager.getTransaction().commit();
-            entityManager.close();
         }
         catch (Exception e) {
             entityManager.getTransaction().rollback();
+        } finally {
             entityManager.close();
-            entityManager = null;
         }
 
         if (Log.doTrace())
@@ -877,8 +878,6 @@
             return null;
         }
         else {
-        	try {
-        		entityManager.getTransaction().begin();
         	profile = new AccountProfileDataBean(userID, password, fullname,
                                                  address, email, creditcard);
             account = new AccountDataBean(0, 0, null, new Timestamp(System.currentTimeMillis()),
openBalance, openBalance, userID);
@@ -887,16 +886,16 @@
             /*
              * managed Transaction
              */
-            
-                
+            try {
+                entityManager.getTransaction().begin();
                 entityManager.persist(profile);
                 entityManager.persist(account);
                 entityManager.getTransaction().commit();
             }
             catch (Exception e) {
                 entityManager.getTransaction().rollback();
+            } finally {
                 entityManager.close();
-                entityManager = null;
             }
 
         }
@@ -972,9 +971,10 @@
         }
         catch (Exception e) {
             entityManager.getTransaction().rollback();
+        } finally {
             entityManager.close();
-            entityManager = null;
         }
+
         return newHolding;
     }
 

Modified: geronimo/daytrader/trunk/modules/entities/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/geronimo/daytrader/trunk/modules/entities/src/main/resources/META-INF/persistence.xml?rev=901896&r1=901895&r2=901896&view=diff
==============================================================================
--- geronimo/daytrader/trunk/modules/entities/src/main/resources/META-INF/persistence.xml
(original)
+++ geronimo/daytrader/trunk/modules/entities/src/main/resources/META-INF/persistence.xml
Thu Jan 21 22:29:06 2010
@@ -17,7 +17,7 @@
 -->
 <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
 
-    <persistence-unit transaction-type="JTA" name="daytrader">
+    <persistence-unit transaction-type="RESOURCE_LOCAL" name="daytrader">
         <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
         <jta-data-source>jdbc/TradeDataSource</jta-data-source>  
         <non-jta-data-source>jdbc/NoTxTradeDataSource</non-jta-data-source> 



Mime
View raw message