Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 136 137 138 139 140 141 < 142 > 143 144 145 146 147 148 .. 166 >> Next

035 {
036 String pName = request.getParameter("productName");
037 String pPrice = request.getParameter("productPrice");
038 String pCount = request.getParameter("productCount");
039
040 if (!validate.validate()) {
041
042 Product newProduct = new Product();
043
044 newProduct.setName(pName);
045 newProduct.setPrice(Double.parseDouble(pPrice));
046 newProduct.setStock(Integer.parseInt(pCount));
047
Listing 46.2 jdoForm.jsp
JDO and Data Persistence 393
The Transaction interface on line 49 provides the operations necessary to perform database transactions—specifically, accessing, creating, and modifying persistent objects and their fields. Prior to performing any database operations, a thread must explicitly create a transaction object or associate itself with an existing transaction object, and that transaction must be open (through a call to begin). All operations that follow the thread are performed under the transaction of the thread.
The operational states of a transaction are either open or closed. An open transaction means that the application has called begin() but has not invoked commit() or abort(). Once an application calls the commit() or abort() methods, the transaction is considered closed. The isOpen() method can be used to determine the state of a transaction. As objects are accessed by an application, read locks are implicitly used to allow proper access to data and write locks are used when objects are modified to ensure that only one transaction is modifying data at one time.
The makePersistent(...) operation on line 52 is executed on an open transaction so that the transient object can be made available for the commit method to make it durable. If the transaction is made available and the operation aborts, then the makePersistent operation is nullified and the target object is marked as transient.
048 // now perform persistence operations
049 Transaction tx = null;
050 // open transaction
051 PersistenceManager pmInstance = pm.getManagerInstance();
052 pmInstance.makePersistent(newProduct);
053 tx = pmInstance.currentTransaction();
054 tx.begin();
055
056 // commit transaction
057 tx.commit();
058
059 System.out.println("successful submission...");
060
061 } else {
062
063 System.out.println("Validation error.");
064
065 }
066 }
067 else if (deleteItem != null)
068 {
069 String[] deleteArray = request.getParameterValues("deleteItems");
070
071 if ((deleteArray != null) && (deleteArray.length > 0))
072 {
073 for (int i=0; i < deleteArray.length; i++)
074 {
075 System.out.println("deleting: " + deleteArray[i]);
076
Listing 46.2 (continued)
394 Item 46
077 Product test = new Product();
078 test.setId(Integer.parseInt(deleteArray[i]));
079
080 PersistenceManager pmInstance = pm.getManagerInstance();
081
082 try
083 {
084 Product toBeDeleted = 2
(Product)pmInstance.getObjectById(test, false);
085 Transaction tx = null;
086 tx = pmInstance.currentTransaction();
087 tx.begin();
088 pmInstance.deletePersistent(toBeDeleted);
089 tx.commit();
090 }
091 catch (Throwable t)
092 {
093 // rollback in case of errors
094 pmInstance.currentTransaction().rollback();
095 t.printStackTrace();
096 }
097 }
098 }
099 }
100
101 %>
102
103 <html>
104 <body>
105
106 <form action="jdoForm.jsp" method=post>
107 <center>
108
109 <table border="0" cellpadding="4" cellspacing="4" width="100%">
110 <tr valign="top" bgcolor="#eeeeee">
111 <td colspan="3">
112 <table cellspacing="0" border="0" width="100%">
113 <tr>
114 <td align="center" valign="top">
115 <table cellspacing="0" cellpadding="1" border="0" 2
width="100%" align="center">
116 <tr>
117 <td valign="top" align="center">
118 <b>JDO Test</b>
119 </td>
120 </tr>
121 </table>
122 </td>
123 </tr>
Listing 46.2 (continued)
JDO and Data Persistence 395
124 </table>
125 </td>
126 </tr>
127 <tr bgcolor="" >
128 <td align="left"><b>Product Name:</b>&nbsp;<b></b>&nbsp;&nbsp;
129 <font size=2 color=red>
130 <%=validate.getErrorMsg("productName")%></font></td>
131 <td align="left">
132 <input type="text" name="productName"
133 value="<%=validate.getProductName()%>" size="30"></td>
134 </tr>
135 <tr bgcolor="" >
136 <td align="left"> <b>Product Price:</b>&nbsp;<b></b>&nbsp;&nbsp;
137 <font size=2 color=red>
138 <%=validate.getErrorMsg("productPrice")%></font></td>
139 <td align="left">
140 <input type="text" name="productPrice"
141 value="<%=validate.getProductPrice()%>" size="30"></td>
142 </tr>
143 <tr bgcolor="" >
144 <td align="left"><b>Product Count:</b>&nbsp;<b></b>&nbsp;&nbsp;
145 <font size=2 color=red>
146 <%=validate.getErrorMsg("productCount")%></font></td>
147 <td align="left">
148 <input type="text" name="productCount"
149 value="<%=validate.getProductCount()%>" size="30"></td>
150 </tr>
151 <tr bgcolor="" >
152 <td align="center" colspan="2">
153 <input type="Submit" name="Add" value="Add">
Previous << 1 .. 136 137 138 139 140 141 < 142 > 143 144 145 146 147 148 .. 166 >> Next