KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBCSQLServerCreateCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.keygen;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27
28 import javax.ejb.EJBException JavaDoc;
29
30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
32 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
33 import org.jboss.ejb.EntityEnterpriseContext;
34 import org.jboss.deployment.DeploymentException;
35
36 /**
37  * Create command for Microsoft SQL Server that uses the value from an IDENTITY
38  * columns. By default uses "SELECT SCOPE_IDENTITY()" to reduce the impact of
39  * triggers; can be overridden with "pk-sql" attribute e.g. for V7.
40  *
41  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
42  * @version $Revision: 37459 $
43  */

44 public class JDBCSQLServerCreateCommand extends JDBCIdentityColumnCreateCommand
45 {
46    protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException
47    {
48       super.initEntityCommand(entityCommand);
49       pkSQL = entityCommand.getAttribute("pk-sql");
50       if(pkSQL == null)
51       {
52          pkSQL = "SELECT SCOPE_IDENTITY()";
53       }
54    }
55
56    protected void initInsertSQL()
57    {
58       super.initInsertSQL();
59       insertSQL = insertSQL + "; " + pkSQL;
60    }
61
62    protected int executeInsert(int index, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
63    {
64       ps.execute();
65       ResultSet JavaDoc rs = null;
66       try
67       {
68          int rows = ps.getUpdateCount();
69          if(rows != 1)
70          {
71             throw new EJBException JavaDoc("Expected updateCount of 1, got " + rows);
72          }
73          if(ps.getMoreResults() == false)
74          {
75             throw new EJBException JavaDoc("Expected ResultSet but got an updateCount. Is NOCOUNT set for all triggers?");
76          }
77
78          rs = ps.getResultSet();
79          if(!rs.next())
80          {
81             throw new EJBException JavaDoc("ResultSet was empty");
82          }
83          pkField.loadInstanceResults(rs, 1, ctx);
84          return rows;
85       }
86       catch(RuntimeException JavaDoc e)
87       {
88          throw e;
89       }
90       catch(Exception JavaDoc e)
91       {
92          // throw EJBException to force a rollback as the row has been inserted
93
throw new EJBException JavaDoc("Error extracting generated keys", e);
94       }
95       finally
96       {
97          JDBCUtil.safeClose(rs);
98       }
99    }
100 }
101
Popular Tags