PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 156 lines | 94 code | 15 blank | 47 comment | 16 complexity | ec2b75c760ed70075010ac8e9b15aca2 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.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import javax.ejb.EJBException;
  26. import org.jboss.as.cmp.CmpMessages;
  27. import org.jboss.as.cmp.context.CmpEntityBeanContext;
  28. import org.jboss.as.cmp.jdbc.bridge.JDBCCMPFieldBridge;
  29. import org.jboss.as.cmp.jdbc.bridge.JDBCEntityBridge;
  30. import org.jboss.as.cmp.jdbc.bridge.JDBCFieldBridge;
  31. import org.jboss.logging.Logger;
  32. /**
  33. * JDBCStoreEntityCommand updates the row with the new state.
  34. * In the event that no field is dirty the command just returns.
  35. * Note: read-only fields are never considered dirty.
  36. *
  37. * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
  38. * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Oberg</a>
  39. * @author <a href="mailto:marc.fleury@telkel.com">Marc Fleury</a>
  40. * @author <a href="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
  41. * @author <a href="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
  42. * @author <a href="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
  43. * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
  44. * @version $Revision: 81030 $
  45. */
  46. public final class JDBCStoreEntityCommand {
  47. private final JDBCEntityBridge entity;
  48. private final JDBCFieldBridge[] primaryKeyFields;
  49. private final Logger log;
  50. public JDBCStoreEntityCommand(JDBCStoreManager manager) {
  51. entity = (JDBCEntityBridge) manager.getEntityBridge();
  52. primaryKeyFields = entity.getPrimaryKeyFields();
  53. // Create the Log
  54. log = Logger.getLogger(
  55. this.getClass().getName() +
  56. "." +
  57. manager.getMetaData().getName());
  58. }
  59. public void execute(CmpEntityBeanContext ctx) {
  60. // scheduled for batch cascade-delete instance should not be updated
  61. // because foreign key fields could be updated to null and cascade-delete will fail.
  62. JDBCEntityBridge.FieldIterator dirtyIterator = entity.getDirtyIterator(ctx);
  63. if (!dirtyIterator.hasNext() || entity.isBeingRemoved(ctx) || entity.isScheduledForBatchCascadeDelete(ctx)) {
  64. if (log.isTraceEnabled()) {
  65. log.trace("Store command NOT executed. Entity is not dirty "
  66. + ", is being removed or scheduled for *batch* cascade delete: pk=" + ctx.getPrimaryKey());
  67. }
  68. return;
  69. }
  70. // generate sql
  71. StringBuffer sql = new StringBuffer(200);
  72. sql.append(SQLUtil.UPDATE)
  73. .append(entity.getQualifiedTableName())
  74. .append(SQLUtil.SET);
  75. SQLUtil.getSetClause(dirtyIterator, sql)
  76. .append(SQLUtil.WHERE);
  77. SQLUtil.getWhereClause(primaryKeyFields, sql);
  78. boolean hasLockedFields = entity.hasLockedFields(ctx);
  79. JDBCEntityBridge.FieldIterator lockedIterator = null;
  80. if (hasLockedFields) {
  81. lockedIterator = entity.getLockedIterator(ctx);
  82. while (lockedIterator.hasNext()) {
  83. sql.append(SQLUtil.AND);
  84. JDBCCMPFieldBridge field = lockedIterator.next();
  85. if (field.getLockedValue(ctx) == null) {
  86. SQLUtil.getIsNullClause(false, field, "", sql);
  87. lockedIterator.remove();
  88. } else {
  89. SQLUtil.getWhereClause(field, sql);
  90. }
  91. }
  92. }
  93. Connection con = null;
  94. PreparedStatement ps = null;
  95. int rowsAffected = 0;
  96. try {
  97. // create the statement
  98. if (log.isDebugEnabled()) {
  99. log.debug("Executing SQL: " + sql);
  100. }
  101. // get the connection
  102. con = entity.getDataSource().getConnection();
  103. ps = con.prepareStatement(sql.toString());
  104. // SET: set the dirty fields parameters
  105. int index = 1;
  106. dirtyIterator.reset();
  107. while (dirtyIterator.hasNext()) {
  108. index = dirtyIterator.next().setInstanceParameters(ps, index, ctx);
  109. }
  110. // WHERE: set primary key fields
  111. index = entity.setPrimaryKeyParameters(ps, index, ctx.getPrimaryKey());
  112. // WHERE: set optimistically locked field values
  113. if (hasLockedFields) {
  114. lockedIterator.reset();
  115. while (lockedIterator.hasNext()) {
  116. JDBCCMPFieldBridge field = lockedIterator.next();
  117. Object value = field.getLockedValue(ctx);
  118. index = field.setArgumentParameters(ps, index, value);
  119. }
  120. }
  121. // execute statement
  122. rowsAffected = ps.executeUpdate();
  123. } catch (EJBException e) {
  124. throw e;
  125. } catch (Exception e) {
  126. throw CmpMessages.MESSAGES.storeFailed(e);
  127. } finally {
  128. JDBCUtil.safeClose(ps);
  129. JDBCUtil.safeClose(con);
  130. }
  131. // check results
  132. if (rowsAffected != 1) {
  133. throw CmpMessages.MESSAGES.updateFailedTooManyRowsAffected(rowsAffected, ctx.getPrimaryKey());
  134. }
  135. // Mark the updated fields as clean.
  136. dirtyIterator.reset();
  137. while (dirtyIterator.hasNext()) {
  138. dirtyIterator.next().setClean(ctx);
  139. }
  140. }
  141. }