001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.keygen;
023:
024: import java.lang.reflect.Field;
025: import java.lang.reflect.Method;
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import javax.ejb.EJBException;
031:
032: import org.jboss.deployment.DeploymentException;
033: import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
034: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
035: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
036: import org.jboss.ejb.EntityEnterpriseContext;
037:
038: /**
039: * Create method that uses the JDBC 3.0 getGeneratedKeys method to obtain
040: * the value from the identity column.
041: *
042: * @author <a href="mailto:jeremy@boynes.com">Jeremy Boynes</a>
043: * @version $Revision: 57209 $
044: */
045: public class JDBC30GeneratedKeysCreateCommand extends
046: JDBCIdentityColumnCreateCommand {
047: private static final Method CONNECTION_PREPARE;
048: private static final Integer GENERATE_KEYS;
049: private static final Method GET_GENERATED_KEYS;
050: static {
051: Method prepare, getGeneratedKeys;
052: Integer generateKeys;
053: try {
054: prepare = Connection.class.getMethod("prepareStatement",
055: new Class[] { String.class, int.class });
056: getGeneratedKeys = PreparedStatement.class.getMethod(
057: "getGeneratedKeys", null);
058: Field f = PreparedStatement.class
059: .getField("RETURN_GENERATED_KEYS");
060: generateKeys = (Integer) f.get(PreparedStatement.class);
061: } catch (Exception e) {
062: prepare = null;
063: getGeneratedKeys = null;
064: generateKeys = null;
065: }
066: CONNECTION_PREPARE = prepare;
067: GET_GENERATED_KEYS = getGeneratedKeys;
068: GENERATE_KEYS = generateKeys;
069: }
070:
071: public void init(JDBCStoreManager manager)
072: throws DeploymentException {
073: if (CONNECTION_PREPARE == null) {
074: throw new DeploymentException(
075: "Create command requires JDBC 3.0 (JDK1.4+)");
076: }
077: super .init(manager);
078: }
079:
080: protected PreparedStatement prepareStatement(Connection c,
081: String sql, EntityEnterpriseContext ctx)
082: throws SQLException {
083: try {
084: return (PreparedStatement) CONNECTION_PREPARE.invoke(c,
085: new Object[] { sql, GENERATE_KEYS });
086: } catch (Exception e) {
087: throw processException(e);
088: }
089: }
090:
091: protected int executeInsert(int paramIndex, PreparedStatement ps,
092: EntityEnterpriseContext ctx) throws SQLException {
093: int rows = ps.executeUpdate();
094: ResultSet rs = null;
095: try {
096: rs = (ResultSet) GET_GENERATED_KEYS.invoke(ps, null);
097: if (!rs.next()) {
098: // throw EJBException to force a rollback as the row has been inserted
099: throw new EJBException(
100: "getGeneratedKeys returned an empty ResultSet");
101: }
102: pkField.loadInstanceResults(rs, 1, ctx);
103: } catch (RuntimeException e) {
104: throw e;
105: } catch (Exception e) {
106: // throw EJBException to force a rollback as the row has been inserted
107: throw new EJBException("Error extracting generated keys", e);
108: } finally {
109: JDBCUtil.safeClose(rs);
110: }
111: return rows;
112: }
113: }
|