int result; int rows; do { // The loop ensures atomicity of the // select + update even for no transaction // or read committed isolation level //sql = query; SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC ); PreparedStatement qps = conn.prepareStatement(query); PreparedStatement ips = null; try { //qps.setString(1, key); ResultSet rs = qps.executeQuery(); boolean isInitialized = rs.next(); if ( !isInitialized ) { result = 0; ips = conn.prepareStatement(insert); //ips.setString(1, key); ips.setInt(1, result); ips.execute(); } else { result = rs.getInt(1); } rs.close(); } catch (SQLException sqle) { log.error("could not read or init a hi value", sqle); throw sqle; } finally { if (ips != null) { ips.close(); } qps.close(); } //sql = update; PreparedStatement ups = conn.prepareStatement(update); try { ups.setInt( 1, result + 1 ); ups.setInt( 2, result ); //ups.setString( 3, key ); rows = ups.executeUpdate(); } catch (SQLException sqle) { log.error("could not update hi value in: " + tableName, sqle); throw sqle; } finally { ups.close(); } } while (rows==0); return new Integer(result);
long result; int rows; do { SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC ); PreparedStatement selectPS = conn.prepareStatement( selectQuery ); try { ResultSet selectRS = selectPS.executeQuery(); if ( !selectRS.next() ) { String err = "could not read a hi value - you need to populate the table: " + tableName; log.error( err ); throw new IdentifierGenerationException( err ); } result = selectRS.getLong( 1 ); selectRS.close(); } catch ( SQLException sqle ) { log.error( "could not read a hi value", sqle ); throw sqle; } finally { selectPS.close(); } SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC ); PreparedStatement updatePS = conn.prepareStatement( updateQuery ); try { int increment = applyIncrementSizeToSourceValues ? incrementSize : 1; updatePS.setLong( 1, result + increment ); updatePS.setLong( 2, result ); rows = updatePS.executeUpdate(); } catch ( SQLException sqle ) { log.error( "could not updateQuery hi value in: " + tableName, sqle ); throw sqle; } finally { updatePS.close(); } } while ( rows == 0 ); accessCounter++; return new Long( result );
Clone fragments detected by clone detection tool
File path: /hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java File path: /hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/id/enhanced/TableStructure.java
Method name: Serializable doWorkInCurrentTransaction(Connection, String) Method name: Serializable doWorkInCurrentTransaction(Connection, String)
Number of AST nodes: 22 Number of AST nodes: 22
1
int result;
1
long result;
2
		int rows;
2
		int rows;
3
		do {
3
		do {
4
			// The loop ensures atomicity of the
5
			// select + update even for no transaction
6
			// or read committed isolation level
7
			//sql = query;
4

8
			SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC );
5
			SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC );
9
			PreparedStatement qps = conn.prepareStatement(query);
6
			PreparedStatement selectPS = conn.prepareStatement(
10
			PreparedStatement ips = null;
11
			try {
12
				//qps.setString(1, key);
7
 selectQuery );
8
			try {
13
				ResultSet rs = qps.executeQuery();
9
				ResultSet selectRS = selectPS.executeQuery();
14
				boolean isInitialized = rs.next();
10
				
15
				if ( !isInitialized ) {
11
if ( !selectRS.next() ) {
16
					result = 0;
12
					
17
					ips = conn.prepareStatement(insert);
18
					//ips.setString(1, key
13
String err = "could not read a hi value - you need to populate the table: " + tableName;
19
);
14
					log.error( err );
20
					ips.setInt(1, result);
15
					
21
					ips.execute();
16
throw new IdentifierGenerationException( err );
22
				}
17
				}
23
				else {
24
					result = rs.getInt(1);
18
				result = 
25
				}
26
				rs
19
selectRS.getLong( 1 );
27
.close();
20
				selectRS.close();
28
			}
21
			}
29
			catch (SQLException sqle) {
22
			catch ( SQLException sqle ) {
30
				log.error("could not read or init a hi value", sqle);
23
				log.error( "could not read a hi value", sqle );
31
				throw sqle;
24
				throw sqle;
32
			}
25
			}
33
			finally {
26
			finally {
34
				if (ips != null) {
27
				
35
					ips.close();
28
selectPS.close();
36
				}
29
			
37
				qps.close();
38
			}
39
			//sql = update;
30
}
31
			SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC );
40
			PreparedStatement ups = conn.prepareStatement(update);
32
			PreparedStatement updatePS = conn.prepareStatement( updateQuery );
41
			try {
33
			try {
42
				ups.setInt( 1, result + 1 );
34
				int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
43
				ups.setInt( 2, result );
35
				updatePS.setLong( 1, result + increment );
44
				//ups.setString( 3, key );
36
				updatePS.setLong( 2, result );
45
				rows = ups.executeUpdate();
37
				rows = updatePS.executeUpdate();
46
			}
38
			}
47
			catch (SQLException sqle) {
39
			catch ( SQLException sqle ) {
48
				log.error("could not update hi value in: " + tableName, sqle);
40
				log.error( "could not updateQuery hi value in: " + tableName, sqle );
49
				throw sqle;
41
				throw sqle;
50
			}
42
			}
51
			finally {
43
			finally {
52
				ups.close();
44
				updatePS.close();
53
			}
45
			}
54
		}
46
		}
55
		while (rows==0);
47
 while ( rows == 0 );
48
		accessCounter++;
49

56
		return new Integer(result);
50
		return new Long( result );
Summary
Number of common nesting structure subtrees1
Number of refactorable cases0
Number of non-refactorable cases1
Time elapsed for finding largest common nesting structure subtrees (ms)0.2
Clones locationClones are in different classes having the same super class
Number of node comparisons76
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements8
    Number of unmapped statements in the first code fragment14
    Number of unmapped statements in the second code fragment14
    Time elapsed for statement mapping (ms)37.5
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
                                  
    1
    long result;
    1
    int result;
    1
    int result;
    2
    int rows;
    Differences
    Expression1Expression2Difference
    resultrowsVARIABLE_NAME_MISMATCH
    2
    int rows;
    2
    int rows;
                            
    21
    do while(rows == 0)
    20
    do while(rows == 0)
    3
    SQL_STATEMENT_LOGGER.logStatement(sql, FormatStyle.BASIC);
    3
    SQL_STATEMENT_LOGGER.logStatement(sql, FormatStyle.BASIC);
    3
    SQL_STATEMENT_LOGGER.logStatement(selectQuery, FormatStyle.BASIC);
    Differences
    Expression1Expression2Difference
    sqlselectQueryVARIABLE_NAME_MISMATCH
    3
    SQL_STATEMENT_LOGGER.logStatement(selectQuery, FormatStyle.BASIC);
    4
    PreparedStatement qps = conn.prepareStatement(query);
    4
    PreparedStatement qps = conn.prepareStatement(query);
    4
    PreparedStatement selectPS = conn.prepareStatement(selectQuery);
    Differences
    Expression1Expression2Difference
    qpsselectPSVARIABLE_NAME_MISMATCH
    queryselectQueryVARIABLE_NAME_MISMATCH
    4
    PreparedStatement selectPS = conn.prepareStatement(selectQuery);
                
    5
    try
    Preondition Violations
    Unmatched statement try cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    5
    try
                                                                                                
    6
    ResultSet selectRS = selectPS.executeQuery();
    Preondition Violations
    Unmatched statement ResultSet selectRS=selectPS.executeQuery(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement ResultSet selectRS=selectPS.executeQuery(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    6
    ResultSet selectRS = selectPS.executeQuery();
                                                  
    7
    if (!selectRS.next())
    Preondition Violations
    Unmatched statement if(!selectRS.next()) cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    7
    if (!selectRS.next())
                                                                                                                                                                                      
    8
    String err = "could not read a hi value - you need to populate the table: " + tableName;
    Preondition Violations
    Unmatched statement String err="could not read a hi value - you need to populate the table: " + tableName; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    8
    String err = "could not read a hi value - you need to populate the table: " + tableName;
                                        
    9
    log.error(err);
                                                                                                      
    10
    throw new IdentifierGenerationException(err);
    Preondition Violations
    Unmatched throw new IdentifierGenerationException(err);
    10
    throw new IdentifierGenerationException(err);
                                                                  
    11
    result = selectRS.getLong(1);
    Preondition Violations
    Unmatched statement result=selectRS.getLong(1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement result=selectRS.getLong(1); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    11
    result = selectRS.getLong(1);
                                              
    12
    selectRS.close();
    Preondition Violations
    Unmatched statement selectRS.close(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    12
    selectRS.close();
    5
    PreparedStatement ips = null;
    5
    PreparedStatement ips = null;
    Preondition Violations
    Unmatched statement PreparedStatement ips=null; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                                
    6
    try
    6
    try
    Preondition Violations
    Unmatched statement try cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                
    7
    ResultSet rs = qps.executeQuery();
    7
    ResultSet rs = qps.executeQuery();
    Preondition Violations
    Unmatched statement ResultSet rs=qps.executeQuery(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement ResultSet rs=qps.executeQuery(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
                                                                          
    8
    boolean isInitialized = rs.next();
    8
    boolean isInitialized = rs.next();
    Preondition Violations
    Unmatched statement boolean isInitialized=rs.next(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement boolean isInitialized=rs.next(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
                                                                          
    9
    if (!isInitialized)
                                              
    10
    result = 0;
    10
    result = 0;
    Preondition Violations
    Unmatched statement result=0; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                              
    11
    ips = conn.prepareStatement(insert);
                                                                                
    12
    ips.setInt(1, result);
                                                      
    13
    ips.execute();
                                        
    else
            
    14
    result = rs.getInt(1);
    14
    result = rs.getInt(1);
    Preondition Violations
    Unmatched statement result=rs.getInt(1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                    
    15
    rs.close();
    15
    rs.close();
    Preondition Violations
    Unmatched statement rs.close(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
                                  
                                                                                                                                              
    13
    SQL_STATEMENT_LOGGER.logStatement(updateQuery, FormatStyle.BASIC);
    16
    PreparedStatement ups = conn.prepareStatement(update);
    16
    PreparedStatement ups = conn.prepareStatement(update);
    14
    PreparedStatement updatePS = conn.prepareStatement(updateQuery);
    Differences
    Expression1Expression2Difference
    upsupdatePSVARIABLE_NAME_MISMATCH
    updateupdateQueryVARIABLE_NAME_MISMATCH
    14
    PreparedStatement updatePS = conn.prepareStatement(updateQuery);
    17
    try
    17
    try
    15
    try
    Differences
    Expression1Expression2Difference
    "could not update hi value in: ""could not updateQuery hi value in: "LITERAL_VALUE_MISMATCH
    upsupdatePSVARIABLE_NAME_MISMATCH
    15
    try
                                                                                                                                                  
    16
    int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
    Preondition Violations
    Unmatched statement int increment=applyIncrementSizeToSourceValues ? incrementSize : 1; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    16
    int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
                                                                                          
    17
    updatePS.setLong(1, result + increment);
    Preondition Violations
    Unmatched statement updatePS.setLong(1,result + increment); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement updatePS.setLong(1,result + increment); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    17
    updatePS.setLong(1, result + increment);
                                                                  
    18
    updatePS.setLong(2, result);
    Preondition Violations
    Unmatched statement updatePS.setLong(2,result); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement updatePS.setLong(2,result); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    18
    updatePS.setLong(2, result);
    18
    ups.setInt(1, result + 1);
    18
    ups.setInt(1, result + 1);
    Preondition Violations
    Unmatched statement ups.setInt(1,result + 1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement ups.setInt(1,result + 1); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
                                                              
    19
    ups.setInt(2, result);
    19
    ups.setInt(2, result);
    Preondition Violations
    Unmatched statement ups.setInt(2,result); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    Unmatched statement ups.setInt(2,result); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
                                                      
    20
    rows = ups.executeUpdate();
    20
    rows = ups.executeUpdate();
    19
    rows = updatePS.executeUpdate();
    Differences
    Expression1Expression2Difference
    upsupdatePSVARIABLE_NAME_MISMATCH
    19
    rows = updatePS.executeUpdate();
                                            
    21
    accessCounter++;
    Preondition Violations
    Unmatched statement accessCounter++; cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    21
    accessCounter++;
    22
    return new Integer(result);
    22
    return new Integer(result);
    22
    return new Long(result);
    Differences
    Expression1Expression2Difference
    java.lang.Integerjava.lang.LongSUBCLASS_TYPE_MISMATCH
    intlongVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Expression new Integer(result) cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression new Long(result) cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Type int of variable result does not match with type long of variable result
    22
    return new Long(result);
    Precondition Violations (32)
    Row Violation
    1Unmatched statement try cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    2Unmatched statement ResultSet selectRS=selectPS.executeQuery(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    3Unmatched statement ResultSet selectRS=selectPS.executeQuery(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    4Unmatched statement if(!selectRS.next()) cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    5Unmatched statement String err="could not read a hi value - you need to populate the table: " + tableName; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    6Unmatched throw new IdentifierGenerationException(err);
    7Unmatched statement result=selectRS.getLong(1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    8Unmatched statement result=selectRS.getLong(1); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    9Unmatched statement selectRS.close(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    10Unmatched statement PreparedStatement ips=null; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    11Unmatched statement try cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    12Unmatched statement ResultSet rs=qps.executeQuery(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    13Unmatched statement ResultSet rs=qps.executeQuery(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    14Unmatched statement boolean isInitialized=rs.next(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    15Unmatched statement boolean isInitialized=rs.next(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    16Unmatched statement result=0; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    17Unmatched statement result=rs.getInt(1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    18Unmatched statement rs.close(); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    19Unmatched statement int increment=applyIncrementSizeToSourceValues ? incrementSize : 1; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    20Unmatched statement updatePS.setLong(1,result + increment); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    21Unmatched statement updatePS.setLong(1,result + increment); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    22Unmatched statement updatePS.setLong(2,result); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    23Unmatched statement updatePS.setLong(2,result); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    24Unmatched statement ups.setInt(1,result + 1); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    25Unmatched statement ups.setInt(1,result + 1); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    26Unmatched statement ups.setInt(2,result); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    27Unmatched statement ups.setInt(2,result); cannot be moved before or after the extracted code, because it throws exception(s) that should be caught by a try block that will be extracted
    28Unmatched statement accessCounter++; cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    29Expression new Integer(result) cannot be parameterized, because it has dependencies to/from statements that will be extracted
    30Expression new Long(result) cannot be parameterized, because it has dependencies to/from statements that will be extracted
    31Type int of variable result does not match with type long of variable result
    32Clone fragment #1 returns variables qps, result, ups , while Clone fragment #2 returns variables selectPS, updatePS