KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > derby > DerbyPkGenerator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba.derby;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.util.Collections JavaDoc;
26
27 import org.apache.cayenne.CayenneException;
28 import org.apache.cayenne.access.DataNode;
29 import org.apache.cayenne.access.QueryLogger;
30 import org.apache.cayenne.dba.JdbcPkGenerator;
31 import org.apache.cayenne.map.DbEntity;
32
33 /**
34  * Default PK generator for Derby that uses updateable ResultSet to get the next id from
35  * the lookup table.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  */

40 public class DerbyPkGenerator extends JdbcPkGenerator {
41
42     static final String JavaDoc SELECT_QUERY = "SELECT NEXT_ID FROM AUTO_PK_SUPPORT"
43             + " WHERE TABLE_NAME = ? FOR UPDATE";
44
45     protected int pkFromDatabase(DataNode node, DbEntity entity) throws Exception JavaDoc {
46
47         if (QueryLogger.isLoggable()) {
48             QueryLogger.logQuery(SELECT_QUERY, Collections
49                     .singletonList(entity.getName()));
50         }
51
52         Connection JavaDoc c = node.getDataSource().getConnection();
53
54         try {
55             PreparedStatement JavaDoc select = c.prepareStatement(
56                     SELECT_QUERY,
57                     ResultSet.TYPE_FORWARD_ONLY,
58                     ResultSet.CONCUR_UPDATABLE);
59
60             select.setString(1, entity.getName());
61             ResultSet JavaDoc rs = select.executeQuery();
62
63             if (!rs.next()) {
64                 throw new CayenneException("PK lookup failed for table: "
65                         + entity.getName());
66             }
67
68             int nextId = rs.getInt(1);
69
70             rs.updateInt(1, nextId + pkCacheSize);
71             rs.updateRow();
72
73             if (rs.next()) {
74                 throw new CayenneException("More than one PK record for table: "
75                         + entity.getName());
76             }
77             
78             rs.close();
79
80             select.close();
81             c.commit();
82
83             return nextId;
84         }
85         finally {
86             c.close();
87         }
88     }
89 }
90
Popular Tags