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