KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > LookupRequest


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: LookupRequest.java,v 1.1 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import com.triactive.jdo.StateManager;
15 import java.sql.Connection JavaDoc;
16 import java.sql.PreparedStatement JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import org.apache.log4j.Category;
20
21
22 class LookupRequest extends Request
23 {
24     private static final Category LOG = Category.getInstance(LookupRequest.class);
25
26     private final String JavaDoc textStmt;
27
28     public LookupRequest(ClassBaseTable table)
29     {
30         super(table);
31
32         FetchStatement fetchStmt = new FetchStatement(table);
33         Column idCol = idMapping.getColumn();
34
35         fetchStmt.select(idCol);
36         fetchStmt.andCondition(fetchStmt.referenceColumn(idCol) + " = ?");
37
38         textStmt = fetchStmt.toString();
39     }
40
41     public void execute(StateManager sm)
42     {
43         PersistenceManager pm = sm.getPersistenceManager();
44
45         try
46         {
47             Connection JavaDoc conn = pm.getConnection(false);
48
49             try
50             {
51                 PreparedStatement JavaDoc ps = conn.prepareStatement(textStmt);
52
53                 try
54                 {
55                     idMapping.setObject(pm, ps, 1, sm.getObjectId());
56
57                     long startTime = System.currentTimeMillis();
58
59                     ResultSet JavaDoc rs = ps.executeQuery();
60
61                     try
62                     {
63                         if (LOG.isDebugEnabled())
64                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + textStmt);
65
66                         if (!rs.next())
67                             throw new ObjectNotFoundException("No such database row", sm.getObject());
68                     }
69                     finally
70                     {
71                         rs.close();
72                     }
73                 }
74                 finally
75                 {
76                     ps.close();
77                 }
78             }
79             finally
80             {
81                 pm.releaseConnection(conn);
82             }
83         }
84         catch (SQLException JavaDoc e)
85         {
86             throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Lookup request failed: " + textStmt, e);
87         }
88     }
89 }
90
Popular Tags