01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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;
25: import java.sql.SQLException;
26: import java.sql.ResultSet;
27:
28: import javax.ejb.EJBException;
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: 57209 $
43: */
44: public class JDBCSQLServerCreateCommand extends
45: JDBCIdentityColumnCreateCommand {
46: protected void initEntityCommand(
47: JDBCEntityCommandMetaData entityCommand)
48: throws DeploymentException {
49: super .initEntityCommand(entityCommand);
50: pkSQL = entityCommand.getAttribute("pk-sql");
51: if (pkSQL == null) {
52: pkSQL = "SELECT SCOPE_IDENTITY()";
53: }
54: }
55:
56: protected void initInsertSQL() {
57: super .initInsertSQL();
58: insertSQL = insertSQL + "; " + pkSQL;
59: }
60:
61: protected int executeInsert(int index, PreparedStatement ps,
62: EntityEnterpriseContext ctx) throws SQLException {
63: ps.execute();
64: ResultSet rs = null;
65: try {
66: int rows = ps.getUpdateCount();
67: if (rows != 1) {
68: throw new EJBException(
69: "Expected updateCount of 1, got " + rows);
70: }
71: if (ps.getMoreResults() == false) {
72: throw new EJBException(
73: "Expected ResultSet but got an updateCount. Is NOCOUNT set for all triggers?");
74: }
75:
76: rs = ps.getResultSet();
77: if (!rs.next()) {
78: throw new EJBException("ResultSet was empty");
79: }
80: pkField.loadInstanceResults(rs, 1, ctx);
81: return rows;
82: } catch (RuntimeException e) {
83: throw e;
84: } catch (Exception e) {
85: // throw EJBException to force a rollback as the row has been inserted
86: throw new EJBException("Error extracting generated keys", e);
87: } finally {
88: JDBCUtil.safeClose(rs);
89: }
90: }
91: }
|