Project

General

Profile

« Previous | Next » 

Revision 7188

share the same dbConnection when inserting and then updating SystemMetadata objects in the backing store.
any errors encountered during the update will rollback the entire transaction and the SM record will not exist, even in part.

View differences:

src/edu/ucsb/nceas/metacat/dataone/SystemMetadataFactory.java
628 628
			}
629 629
            
630 630
            //insert the systemmetadata object or just update it as needed
631
            boolean exists = IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue());
632
            if (!exists) {
633
            	IdentifierManager.getInstance().insertSystemMetadata(sm);
634
            	logMetacat.info("Generated SystemMetadata for " + localId);
635
            } else {
636
            	IdentifierManager.getInstance().updateSystemMetadata(sm);
637
            	logMetacat.info("Updated SystemMetadata for " + localId);
638
            }
631
        	IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
632
        	logMetacat.info("Generated or Updated SystemMetadata for " + localId);
633
            
639 634
        	logMetacat.trace("METRICS:\tGENERATE_SYSTEM_METADATA:\tEND:\tLOCALID:\t" + localId);
640 635

  
641 636
        }
src/edu/ucsb/nceas/metacat/dataone/hazelcast/HazelcastService.java
442 442
	private void saveLocally(SystemMetadata sm) {
443 443
		logMetacat.debug("Saving entry locally: " + sm.getIdentifier().getValue());
444 444
		try {
445
			if (!IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue())) {
446
				IdentifierManager.getInstance().insertSystemMetadata(sm);
447
				
448
			} else {
449
				IdentifierManager.getInstance().updateSystemMetadata(sm);
450
				
451
			}
445

  
446
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
447

  
452 448
		} catch (McdbDocNotFoundException e) {
453 449
			logMetacat.error("Could not save System Metadata to local store.", e);
454 450
			
src/edu/ucsb/nceas/metacat/dataone/hazelcast/SystemMetadataMap.java
41 41
	public void store(Identifier pid, SystemMetadata sm) {
42 42
		try {
43 43
			logMetacat.debug("Storing System Metadata to store: " + pid.getValue());
44
			if (!IdentifierManager.getInstance().systemMetadataExists(pid.getValue())) {
45
				IdentifierManager.getInstance().insertSystemMetadata(sm);
46
				
47
			} else {
48
				IdentifierManager.getInstance().updateSystemMetadata(sm);
49
				
50
			}
51
			
44
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
52 45
		} catch (McdbDocNotFoundException e) {
53 46
			throw new RuntimeException(e.getMessage(), e);
54 47
			
src/edu/ucsb/nceas/metacat/IdentifierManager.java
39 39
import org.dataone.client.ObjectFormatCache;
40 40
import org.dataone.service.exceptions.BaseException;
41 41
import org.dataone.service.exceptions.InvalidSystemMetadata;
42
import org.dataone.service.exceptions.NotFound;
43
import org.dataone.service.exceptions.NotImplemented;
44
import org.dataone.service.exceptions.ServiceFailure;
45 42
import org.dataone.service.types.v1.AccessPolicy;
46 43
import org.dataone.service.types.v1.AccessRule;
47 44
import org.dataone.service.types.v1.Checksum;
......
959 956
     * @throws SQLException 
960 957
     * @throws InvalidSystemMetadata 
961 958
     */
962
    public void insertSystemMetadata(SystemMetadata sysmeta) 
959
    public void insertOrUpdateSystemMetadata(SystemMetadata sysmeta) 
963 960
        throws McdbDocNotFoundException, SQLException, InvalidSystemMetadata {
964 961
    	String guid = sysmeta.getIdentifier().getValue();
965
    	// insert the record
966
        insertSystemMetadata(guid);
967
        // update with the values
968
        updateSystemMetadata(sysmeta);
962
    	
963
    	 // Get a database connection from the pool
964
        DBConnection dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertSystemMetadata");
965
        int serialNumber = dbConn.getCheckOutSerialNumber();
969 966
        
967
        try {
968
        	// use a single transaction for it all
969
        	dbConn.setAutoCommit(false);
970
        	
971
	    	// insert the record if needed
972
        	if (!IdentifierManager.getInstance().systemMetadataExists(guid)) {
973
    	        insertSystemMetadata(guid, dbConn);
974
			}
975
	        // update with the values
976
	        updateSystemMetadata(sysmeta, dbConn);
977
	        
978
	        // commit if we got here with no errors
979
	        dbConn.commit();
980
        } catch (Exception e) {
981
            e.printStackTrace();
982
            logMetacat.error("Error while creating " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
983
            dbConn.rollback();
984
        } finally {
985
            // Return database connection to the pool
986
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
987
        }
988
        
989
        
970 990
    }
971 991
        
972 992
    
......
1029 1049
        String authoritativeMemberNode, long modifiedDate, String submitter, 
1030 1050
        String guid, String objectFormat, BigInteger size, boolean archived,
1031 1051
        boolean replicationAllowed, int numberReplicas, String obsoletes,
1032
        String obsoletedBy, BigInteger serialVersion) throws SQLException  {
1052
        String obsoletedBy, BigInteger serialVersion, DBConnection dbConn) throws SQLException  {
1053
  
1054
        // Execute the insert statement
1055
        String query = "update " + TYPE_SYSTEM_METADATA + 
1056
            " set (date_uploaded, rights_holder, checksum, checksum_algorithm, " +
1057
            "origin_member_node, authoritive_member_node, date_modified, " +
1058
            "submitter, object_format, size, archived, replication_allowed, number_replicas, " +
1059
            "obsoletes, obsoleted_by, serial_version) " +
1060
            "= (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) where guid = ?";
1061
        PreparedStatement stmt = dbConn.prepareStatement(query);
1033 1062
        
1034
        DBConnection dbConn = null;
1035
        int serialNumber = -1;
1063
        //data values
1064
        stmt.setTimestamp(1, new java.sql.Timestamp(dateUploaded));
1065
        stmt.setString(2, rightsHolder);
1066
        stmt.setString(3, checksum);
1067
        stmt.setString(4, checksumAlgorithm);
1068
        stmt.setString(5, originMemberNode);
1069
        stmt.setString(6, authoritativeMemberNode);
1070
        stmt.setTimestamp(7, new java.sql.Timestamp(modifiedDate));
1071
        stmt.setString(8, submitter);
1072
        stmt.setString(9, objectFormat);
1073
        stmt.setString(10, size.toString());
1074
        stmt.setBoolean(11, archived);
1075
        stmt.setBoolean(12, replicationAllowed);
1076
        stmt.setInt(13, numberReplicas);
1077
        stmt.setString(14, obsoletes);
1078
        stmt.setString(15, obsoletedBy);
1079
        stmt.setString(16, serialVersion.toString());
1080

  
1081
        //where clause
1082
        stmt.setString(17, guid);
1083
        logMetacat.debug("stmt: " + stmt.toString());
1084
        //execute
1085
        int rows = stmt.executeUpdate();
1086

  
1087
        stmt.close();
1088
               
1089
    }
1090
    
1091
    private void insertReplicationPolicy(String guid, String policy, List<String> memberNodes, DBConnection dbConn) throws SQLException
1092
    {
1093
           
1094
        // remove existing values first
1095
        String delete = "delete from systemMetadataReplicationPolicy " + 
1096
        "where guid = ? and policy = ?";
1097
        PreparedStatement stmt = dbConn.prepareStatement(delete);
1098
        //data values
1099
        stmt.setString(1, guid);
1100
        stmt.setString(2, policy);
1101
        //execute
1102
        int deletedCount = stmt.executeUpdate();
1103
        stmt.close();
1036 1104
        
1037
        try {
1038
            // Get a database connection from the pool
1039
            dbConn = 
1040
                DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1041
            serialNumber = dbConn.getCheckOutSerialNumber();
1042

  
1105
        for (String memberNode: memberNodes) {
1043 1106
            // Execute the insert statement
1044
            String query = "update " + TYPE_SYSTEM_METADATA + 
1045
                " set (date_uploaded, rights_holder, checksum, checksum_algorithm, " +
1046
                "origin_member_node, authoritive_member_node, date_modified, " +
1047
                "submitter, object_format, size, archived, replication_allowed, number_replicas, " +
1048
                "obsoletes, obsoleted_by, serial_version) " +
1049
                "= (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) where guid = ?";
1050
            PreparedStatement stmt = dbConn.prepareStatement(query);
1107
            String insert = "insert into systemMetadataReplicationPolicy " + 
1108
                "(guid, policy, member_node) " +
1109
                "values (?, ?, ?)";
1110
            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1051 1111
            
1052 1112
            //data values
1053
            stmt.setTimestamp(1, new java.sql.Timestamp(dateUploaded));
1054
            stmt.setString(2, rightsHolder);
1055
            stmt.setString(3, checksum);
1056
            stmt.setString(4, checksumAlgorithm);
1057
            stmt.setString(5, originMemberNode);
1058
            stmt.setString(6, authoritativeMemberNode);
1059
            stmt.setTimestamp(7, new java.sql.Timestamp(modifiedDate));
1060
            stmt.setString(8, submitter);
1061
            stmt.setString(9, objectFormat);
1062
            stmt.setString(10, size.toString());
1063
            stmt.setBoolean(11, archived);
1064
            stmt.setBoolean(12, replicationAllowed);
1065
            stmt.setInt(13, numberReplicas);
1066
            stmt.setString(14, obsoletes);
1067
            stmt.setString(15, obsoletedBy);
1068
            stmt.setString(16, serialVersion.toString());
1113
            insertStatement.setString(1, guid);
1114
            insertStatement.setString(2, policy);
1115
            insertStatement.setString(3, memberNode);
1116
            
1117
            logMetacat.debug("systemMetadataReplicationPolicy sql: " + insertStatement.toString());
1069 1118

  
1070
            //where clause
1071
            stmt.setString(17, guid);
1072
            logMetacat.debug("stmt: " + stmt.toString());
1073 1119
            //execute
1074
            int rows = stmt.executeUpdate();
1075

  
1076
            stmt.close();
1077
            
1078
        } catch (SQLException e) {
1079
            logMetacat.error("updateSystemMetadataFields: SQL error while updating system metadata: " 
1080
                    + e.getMessage());
1081
            throw e;
1082
            
1083
        } finally {
1084
            // Return database connection to the pool
1085
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1120
            int rows = insertStatement.executeUpdate();
1121
            insertStatement.close();
1086 1122
        }
1123
        
1087 1124
    }
1088 1125
    
1089
    private void insertReplicationPolicy(String guid, String policy, List<String> memberNodes)
1090
    {
1091
        DBConnection dbConn = null;
1092
        int serialNumber = -1;
1126
    private void insertReplicationStatus(String guid, List<Replica> replicas, DBConnection dbConn) throws SQLException {
1127
       
1128
        // remove existing values first
1129
        String delete = "delete from systemMetadataReplicationStatus " + 
1130
        "where guid = ?";
1131
        PreparedStatement stmt = dbConn.prepareStatement(delete);
1132
        //data values
1133
        stmt.setString(1, guid);
1134
        //execute
1135
        int deletedCount = stmt.executeUpdate();
1136
        stmt.close();
1093 1137
        
1094
        try {
1095
            // Get a database connection from the pool
1096
            dbConn = 
1097
                DBConnectionPool.getDBConnection("IdentifierManager.insertReplicationPolicy");
1098
            serialNumber = dbConn.getCheckOutSerialNumber();
1099

  
1100
            // remove existing values first
1101
            String delete = "delete from systemMetadataReplicationPolicy " + 
1102
            "where guid = ? and policy = ?";
1103
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1104
	        //data values
1105
	        stmt.setString(1, guid);
1106
	        stmt.setString(2, policy);
1107
	        //execute
1108
	        int deletedCount = stmt.executeUpdate();
1109
	        stmt.close();
1110
            
1111
            for (String memberNode: memberNodes) {
1138
        if (replicas != null) {
1139
            for (Replica replica: replicas) {
1112 1140
	            // Execute the insert statement
1113
	            String insert = "insert into systemMetadataReplicationPolicy " + 
1114
	                "(guid, policy, member_node) " +
1115
	                "values (?, ?, ?)";
1141
	            String insert = "insert into systemMetadataReplicationStatus " + 
1142
	                "(guid, member_node, status, date_verified) " +
1143
	                "values (?, ?, ?, ?)";
1116 1144
	            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1117 1145
	            
1118 1146
	            //data values
1147
	            String memberNode = replica.getReplicaMemberNode().getValue();
1148
	            String status = replica.getReplicationStatus().toString();
1149
	            java.sql.Date sqlDate = new java.sql.Date(replica.getReplicaVerified().getTime());
1119 1150
	            insertStatement.setString(1, guid);
1120
	            insertStatement.setString(2, policy);
1121
	            insertStatement.setString(3, memberNode);
1151
	            insertStatement.setString(2, memberNode);
1152
	            insertStatement.setString(3, status);
1153
	            insertStatement.setDate(4, sqlDate);
1154

  
1155
	            logMetacat.debug("systemMetadataReplicationStatus sql: " + insertStatement.toString());
1122 1156
	            
1123
	            logMetacat.debug("systemMetadataReplicationPolicy sql: " + insertStatement.toString());
1124

  
1125 1157
	            //execute
1126 1158
	            int rows = insertStatement.executeUpdate();
1127 1159
	            insertStatement.close();
1128 1160
            }
1129
        } catch (SQLException e) {
1130
            logMetacat.error("SQL error while adding systemMetadataReplicationPolicy for: " + guid, e); 
1131
        } finally {
1132
            // Return database connection to the pool
1133
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1134 1161
        }
1162
       
1135 1163
    }
1136 1164
    
1137
    private void insertReplicationStatus(String guid, List<Replica> replicas) {
1138
        DBConnection dbConn = null;
1139
        int serialNumber = -1;
1140
        
1141
        try {
1142
            // Get a database connection from the pool
1143
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertReplicas");
1144
            serialNumber = dbConn.getCheckOutSerialNumber();
1145

  
1146
            // remove existing values first
1147
            String delete = "delete from systemMetadataReplicationStatus " + 
1148
            "where guid = ?";
1149
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1150
	        //data values
1151
	        stmt.setString(1, guid);
1152
	        //execute
1153
	        int deletedCount = stmt.executeUpdate();
1154
	        stmt.close();
1155
            
1156
	        if (replicas != null) {
1157
	            for (Replica replica: replicas) {
1158
		            // Execute the insert statement
1159
		            String insert = "insert into systemMetadataReplicationStatus " + 
1160
		                "(guid, member_node, status, date_verified) " +
1161
		                "values (?, ?, ?, ?)";
1162
		            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1163
		            
1164
		            //data values
1165
		            String memberNode = replica.getReplicaMemberNode().getValue();
1166
		            String status = replica.getReplicationStatus().toString();
1167
		            java.sql.Date sqlDate = new java.sql.Date(replica.getReplicaVerified().getTime());
1168
		            insertStatement.setString(1, guid);
1169
		            insertStatement.setString(2, memberNode);
1170
		            insertStatement.setString(3, status);
1171
		            insertStatement.setDate(4, sqlDate);
1172
	
1173
		            logMetacat.debug("systemMetadataReplicationStatus sql: " + insertStatement.toString());
1174
		            
1175
		            //execute
1176
		            int rows = insertStatement.executeUpdate();
1177
		            insertStatement.close();
1178
	            }
1179
	        }
1180
        } catch (SQLException e) {
1181
            logMetacat.error("SQL error while adding systemMetadataReplicationStatus for: " + guid, e); 
1182
        } finally {
1183
            // Return database connection to the pool
1184
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1185
        }
1186
    }
1187
    
1188 1165
    /**
1189 1166
     * Insert the system metadata fields into the db
1190 1167
     * @param sm
1191 1168
     * @throws McdbDocNotFoundException 
1192 1169
     * @throws SQLException 
1193 1170
     * @throws InvalidSystemMetadata 
1171
     * @throws AccessException 
1194 1172
     */
1195
    public void updateSystemMetadata(SystemMetadata sm) 
1196
      throws McdbDocNotFoundException, SQLException, InvalidSystemMetadata {
1173
    public void updateSystemMetadata(SystemMetadata sm, DBConnection dbConn) 
1174
      throws McdbDocNotFoundException, SQLException, InvalidSystemMetadata, AccessException {
1197 1175
    	
1198 1176
      Boolean replicationAllowed = false;
1199 1177
		  Integer numberReplicas = -1;
......
1223 1201
		    numberReplicas,
1224 1202
		    sm.getObsoletes() == null ? null:sm.getObsoletes().getValue(),
1225 1203
		    sm.getObsoletedBy() == null ? null: sm.getObsoletedBy().getValue(),
1226
		    sm.getSerialVersion()
1204
		    sm.getSerialVersion(),
1205
		    dbConn
1227 1206
        );
1228 1207
        
1229 1208
        String guid = sm.getIdentifier().getValue();
......
1240 1219
			    for (NodeReference node: replicationPolicy.getBlockedMemberNodeList()) {
1241 1220
			    	nodes.add(node.getValue());
1242 1221
			    }
1243
			    this.insertReplicationPolicy(guid, policy, nodes);
1222
			    this.insertReplicationPolicy(guid, policy, nodes, dbConn);
1244 1223
		    }
1245 1224
		    
1246 1225
		    if (replicationPolicy.getPreferredMemberNodeList() != null) {
......
1249 1228
			    for (NodeReference node: replicationPolicy.getPreferredMemberNodeList()) {
1250 1229
			    	nodes.add(node.getValue());
1251 1230
			    }
1252
		        this.insertReplicationPolicy(guid, policy, nodes);
1231
		        this.insertReplicationPolicy(guid, policy, nodes, dbConn);
1253 1232
		    }
1254 1233
        }
1255 1234
        
1256 1235
        // save replica information
1257
        this.insertReplicationStatus(guid, sm.getReplicaList());
1236
        this.insertReplicationStatus(guid, sm.getReplicaList(), dbConn);
1258 1237
        
1259 1238
        // save access policy
1260 1239
        AccessPolicy accessPolicy = sm.getAccessPolicy();
1261 1240
        if (accessPolicy != null) {
1262
        	try {
1263
				this.insertAccessPolicy(guid, accessPolicy);
1264
			} catch (AccessException e) {
1265
				throw new McdbDocNotFoundException(e);
1266
			}
1241
			this.insertAccessPolicy(guid, accessPolicy);
1267 1242
        }
1268 1243
    }
1269 1244
    
......
1718 1693
    /**
1719 1694
     * create the systemmetadata record
1720 1695
     * @param guid
1696
     * @param dbConn 
1697
     * @throws SQLException 
1721 1698
     */
1722
    private void insertSystemMetadata(String guid)
1699
    private void insertSystemMetadata(String guid, DBConnection dbConn) throws SQLException
1723 1700
    {        
1724
        
1725
        int serialNumber = -1;
1726
        DBConnection dbConn = null;
1727
        try {
1728 1701

  
1729
            // Get a database connection from the pool
1730
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertSystemMetadata");
1731
            serialNumber = dbConn.getCheckOutSerialNumber();
1702
        // Execute the insert statement
1703
        String query = "insert into " + TYPE_SYSTEM_METADATA + " (guid) values (?)";
1704
        PreparedStatement stmt = dbConn.prepareStatement(query);
1705
        stmt.setString(1, guid);
1706
        logMetacat.debug("system metadata query: " + stmt.toString());
1707
        int rows = stmt.executeUpdate();
1732 1708

  
1733
            // Execute the insert statement
1734
            String query = "insert into " + TYPE_SYSTEM_METADATA + " (guid) values (?)";
1735
            PreparedStatement stmt = dbConn.prepareStatement(query);
1736
            stmt.setString(1, guid);
1737
            logMetacat.debug("system metadata query: " + stmt.toString());
1738
            int rows = stmt.executeUpdate();
1739

  
1740
            stmt.close();
1741
        } catch (Exception e) {
1742
            e.printStackTrace();
1743
            logMetacat.error("Error while creating " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
1744
        } finally {
1745
            // Return database connection to the pool
1746
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1747
        }
1709
        stmt.close();
1710
        
1748 1711
    }
1749 1712
    
1750 1713
    public void deleteSystemMetadata(String guid)

Also available in: Unified diff