PageRenderTime 32ms CodeModel.GetById 21ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCIdentityColumnCreateCommand.java

#
Java | 102 lines | 60 code | 6 blank | 36 comment | 6 complexity | 73877d9d721b605ddaf006de67e146d3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2011, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a 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 */
 22package org.jboss.as.cmp.jdbc;
 23
 24import java.lang.reflect.InvocationTargetException;
 25import java.sql.Connection;
 26import java.sql.PreparedStatement;
 27import java.sql.ResultSet;
 28import java.sql.SQLException;
 29import java.sql.Statement;
 30import static org.jboss.as.cmp.CmpMessages.MESSAGES;
 31import org.jboss.as.cmp.jdbc.bridge.JDBCCMPFieldBridge;
 32import org.jboss.as.cmp.jdbc.bridge.JDBCFieldBridge;
 33import org.jboss.as.cmp.context.CmpEntityBeanContext;
 34
 35/**
 36 * Base class for create commands where the PK value is generated as side
 37 * effect of performing the insert operation. This is typically associated
 38 * with database platforms that use identity columns
 39 *
 40 * @author <a href="mailto:jeremy@boynes.com">Jeremy Boynes</a>
 41 */
 42public abstract class JDBCIdentityColumnCreateCommand extends JDBCAbstractCreateCommand {
 43    protected JDBCCMPFieldBridge pkField;
 44    protected String pkSQL;
 45
 46    protected boolean isInsertField(JDBCFieldBridge field) {
 47        // do not include PK fields in the insert
 48        return super.isInsertField(field) && !field.isPrimaryKeyMember();
 49    }
 50
 51    protected void initGeneratedFields() {
 52        super.initGeneratedFields();
 53        pkField = getGeneratedPKField();
 54    }
 55
 56    protected int executeInsert(int paramIndex, PreparedStatement ps, CmpEntityBeanContext ctx) throws SQLException {
 57        int rows = ps.executeUpdate();
 58        Connection c;
 59        Statement s = null;
 60        ResultSet rs = null;
 61        try {
 62            c = ps.getConnection();
 63            s = c.createStatement();
 64            rs = s.executeQuery(pkSQL);
 65            if (!rs.next()) {
 66                throw MESSAGES.resultSetEmpty();
 67            }
 68            pkField.loadInstanceResults(rs, 1, ctx);
 69        } catch (RuntimeException e) {
 70            throw e;
 71        } catch (Exception e) {
 72            // throw EJBException to force a rollback as the row has been inserted
 73            throw MESSAGES.errorExtractingGeneratedKey(e);
 74        } finally {
 75            JDBCUtil.safeClose(rs);
 76            JDBCUtil.safeClose(s);
 77        }
 78        return rows;
 79    }
 80
 81    /**
 82     * Helper for subclasses that use reflection to avoid driver dependencies.
 83     *
 84     * @param t an Exception raised by a reflected call
 85     * @return SQLException extracted from the Throwable
 86     */
 87    protected SQLException processException(Throwable t) {
 88        if (t instanceof InvocationTargetException) {
 89            t = ((InvocationTargetException) t).getTargetException();
 90        }
 91        if (t instanceof SQLException) {
 92            return (SQLException) t;
 93        }
 94        if (t instanceof RuntimeException) {
 95            throw (RuntimeException) t;
 96        }
 97        if (t instanceof Error) {
 98            throw (Error) t;
 99        }
100        throw new IllegalStateException(t);
101    }
102}