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);
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); try { ResultSet rs = qps.executeQuery(); if ( !rs.next() ) { String err = "could not read a hi value - you need to populate the table: " + tableName; log.error(err); throw new IdentifierGenerationException(err); } result = rs.getInt(1); rs.close(); } catch (SQLException sqle) { log.error("could not read a hi value", sqle); throw sqle; } finally { qps.close(); } sql = update; SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC ); PreparedStatement ups = conn.prepareStatement(update); try { ups.setInt( 1, result + 1 ); ups.setInt( 2, result ); 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);
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/TableGenerator.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
int result;
2
		int rows;
2
		int rows;
3
		do {
3
		do {
4
			// The loop ensures atomicity of the
4
			// The loop ensures atomicity of the
5
			// select + update even for no transaction
5
			// select + update even for no transaction
6
			// or read committed isolation level
6
			// or read committed isolation level
7
			//sql = query;
7
			sql = query;
8
			SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC );
8
			SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC );
9
			PreparedStatement qps = conn.prepareStatement(query);
9
			PreparedStatement qps = conn.prepareStatement(query);
10
			PreparedStatement ips = null;
10
			
11
			try {
12
				//qps.setString(1, key);
13
				ResultSet rs = qps.executeQuery();
14
				boolean isInitialized = rs.next();
15
				if ( !isInitialized ) {
16
					result = 0;
17
					ips = conn.prepareStatement(insert);
18
					//ips.setString(1, key);
19
					ips.setInt(1, result);
20
					ips.execute(
11
try {
12
				ResultSet rs = qps.executeQuery();
13
				if ( !rs.next() ) {
14
					String err = "could not read a hi value - you need to populate the table: " + tableName;
15
					log.error(err);
21
);
16
					throw new IdentifierGenerationException(err);
22
				}
17
				}
23
				else {
18
				
24
					result = rs.getInt(1);
19
result = rs.getInt(1);
25
				}
20
				
26
				rs.close();
21
rs.close();
27
			}
22
			}
28
			catch (SQLException sqle) {
23
			catch (SQLException sqle) {
29
				log.error("could not read or init a hi value", sqle);
24
				log.error("could not read a hi value", sqle);
30
				throw sqle;
25
				throw sqle;
31
			}
26
			}
32
			finally {
27
			finally {
33
				if (ips != null) {
28
				
34
					ips.close();
29
qps.close();
35
				}
30
			}
36
				qps.close();
31
			
37
			}
38
			//sql = update
32
sql = update;
39
;
33
			SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC );
40
			PreparedStatement ups = conn.prepareStatement(update);
34
			PreparedStatement ups = conn.prepareStatement(update);
41
			try {
35
			try {
42
				ups.setInt( 1, result + 1 );
36
				ups.setInt( 1, result + 1 );
43
				ups.setInt( 2, result );
37
				ups.setInt( 2, result );
44
				//ups.setString( 3, key );
38
				
45
				rows = ups.executeUpdate();
39
rows = ups.executeUpdate();
46
			}
40
			}
47
			catch (SQLException sqle) {
41
			catch (SQLException sqle) {
48
				log.error("could not update hi value in: " + tableName, sqle);
42
				log.error("could not update hi value in: " + tableName, sqle);
49
				throw sqle;
43
				throw sqle;
50
			}
44
			}
51
			finally {
45
			finally {
52
				ups.close();
46
				ups.close();
53
			}
47
			}
54
		}
48
		}
55
		while (rows==0);
49
		while (rows==0);
56
		return new Integer(result);
50
		return new Integer(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 comparisons79
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements11
    Number of unmapped statements in the first code fragment11
    Number of unmapped statements in the second code fragment11
    Time elapsed for statement mapping (ms)12.9
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    1
    int result;
    1
    int result;
    2
    int rows;
    2
    int rows;
    21
    do while(rows == 0)
    21
    do while(rows == 0)
                              
    3
    sql = query;
    3
    SQL_STATEMENT_LOGGER.logStatement(sql, FormatStyle.BASIC);
    4
    SQL_STATEMENT_LOGGER.logStatement(sql, FormatStyle.BASIC);
    4
    PreparedStatement qps = conn.prepareStatement(query);
    5
    PreparedStatement qps = conn.prepareStatement(query);
    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
    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
    6
    try
                                                                          
    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
    7
    ResultSet rs = qps.executeQuery();
                                      
    8
    if (!rs.next())
    Preondition Violations
    Unmatched statement if(!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
    8
    if (!rs.next())
                                                                                                                                                                                      
    9
    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
    9
    String err = "could not read a hi value - you need to populate the table: " + tableName;
                                          
    10
    log.error(err);
                                                                                                      
    11
    throw new IdentifierGenerationException(err);
    Preondition Violations
    Unmatched throw new IdentifierGenerationException(err);
    11
    throw new IdentifierGenerationException(err);
                                                    
    12
    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
    Unmatched statement result=rs.getInt(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
    12
    result = rs.getInt(1);
                                  
    13
    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
    rs.close();
    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
                                  
                                  
    14
    sql = update;
    Preondition Violations
    Unmatched statement sql=update; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    14
    sql = update;
                                                                                                                              
    15
    SQL_STATEMENT_LOGGER.logStatement(sql, FormatStyle.BASIC);
    16
    PreparedStatement ups = conn.prepareStatement(update);
    16
    PreparedStatement ups = conn.prepareStatement(update);
    17
    try
    17
    try
    18
    ups.setInt(1, result + 1);
    18
    ups.setInt(1, result + 1);
    19
    ups.setInt(2, result);
    19
    ups.setInt(2, result);
    20
    rows = ups.executeUpdate();
    20
    rows = ups.executeUpdate();
    22
    return new Integer(result);
    22
    return new Integer(result);
    Precondition Violations (20)
    Row Violation
    1Unmatched statement PreparedStatement ips=null; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    2Unmatched statement try cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    3Unmatched 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
    4Unmatched 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
    5Unmatched statement if(!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
    6Unmatched 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
    7Unmatched throw new IdentifierGenerationException(err);
    8Unmatched 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
    9Unmatched statement result=rs.getInt(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
    10Unmatched 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
    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 sql=update; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    20The refactoring of the clones is infeasible, because classes org.hibernate.id.MultipleHiLoPerTableGenerator and org.hibernate.id.TableGenerator do not have a common superclass