英文:
Hibernate @Enumerated as AtomicReference
问题
我有一个使用Hibernate进行数据库操作的类,需要它是线程安全的。我正在尝试重构代码,使用原子变量及其操作来替代同步方法,以避免由于锁定而导致的性能下降问题(涉及大量线程),但我在将带有@Enumeration的变量重构为AtomicReference时遇到了异常:
> 属性[com.gateway.domain.ImportTypeDetail.importFrequency]被标注为枚举类型,但其Java类型不是枚举类型
> [java.util.concurrent.atomic.AtomicReference]
重构前的代码:
@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private ImportFrequency importFrequency;
我重构为:
@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private AtomicReference<ImportFrequency> importFrequency = new AtomicReference<>(ImportFrequency.UNKNOWN);
在这种情况下是否可以实现AtomicReference?EnumType的唯一选项是`ORDINAL`和`STRING`,用于类型属性或整数和类型属性或字符串。
英文:
I have a class that uses Hiberante for database operations and need it to be thread-safe. I am trying refactor the code to use Atomic Variables with its Operations instead of having a synchronized method due to locking as it is breaking the application due to slow performance (a lot of threads are in play) on a high volume of requests but I am having issues with a variable with @Enumeration to refactor to AtomicReference due to an exception:
> Attribute [com.gateway.domain.ImportTypeDetail.importFrequency] was
> annotated as enumerated, but its java type is not an enum
> [java.util.concurrent.atomic.AtomicReference]
Code before refactor:
@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private ImportFrequency importFrequency;
I refactored to:
@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private AtomicReference<ImportFrequency> importFrequency = new AtomicReference<>(ImportFrequency.UNKNOWN);
Is it possible to implement AtomicReference in the scenario? EnumType only options are ORDINAL
and STRING
for type property or integer and type property or string respectively.
专注分享java语言的经验与见解,让所有开发者获益!
评论