增加執行時間,我有以下實體:柱上添加@Convert由超過10倍
@Data
@Entity
@Table(name="\"Customer\"")
public class Customer {
@Id
@SequenceGenerator(name="customer_id_seq",
sequenceName="customer_id_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="customer_id_seq")
private long id;
private String firstName;
@Convert(converter = LastNameEncryption.class)
private String lastName;
}
其中LastNameEncryption.java
是:
public class LastNameEncryption implements AttributeConverter<String,String> {
private static SecretKeySpec secretKey;
private final static String peselKey = "somekey";
@Override
public String convertToDatabaseColumn(String attribute) {
try
{
setKey();
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(attribute.getBytes("UTF-8")));
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public String convertToEntityAttribute(String dbData) {
try {
setKey();
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(dbData)));
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
public static void setKey() {
MessageDigest sha = null;
byte[] key;
try {
key = peselKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
這應是一個ecnryption /解密機制行事。
但是,當我添加這個時,簡單查詢的執行時間從200ms左右增加到5.5s(原始主題here)。然後我發現,當我註釋@Comment(...)
註釋時,查詢再次平穩運行。
我是否犯了錯誤,或者這是正常行爲?
注意
當我比較的執行時間,只有3表中的實體。這裏是方法的執行時間日誌:
Execution time of convertToEntityAttribute: 5193
Execution time of convertToEntityAttribute: 0
Execution time of convertToEntityAttribute: 0
Execution time of convertToEntityAttribute: 0
出於某種原因,它需要幾乎5.2s到首次加密 - 那麼,時間小於1毫秒小。
您是否嘗試檢查轉換器的時間? –
@AmerQarabsa,我發佈了執行時間日誌 – uksz