java - Hibernate @Enumerated 为 AtomicReference
问题描述
我有一个使用 Hiberante 进行数据库操作的类,并且需要它是线程安全的。我正在尝试重构代码以使用原子变量及其操作,而不是由于锁定而使用同步方法,因为由于大量请求的性能缓慢(很多线程正在运行)而导致应用程序中断,但我是由于异常,使用 @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
类型属性或整数和类型属性或字符串。
解决方案
推荐阅读
- python - 在 Django 测试中匹配查询集
- tableau-api - Tableau 中的总工作日计算
- php - PHP - 如果元素每条记录具有相似的标签,则获取正确的 XML 值
- haskell - 使用“外来”作为变量/绑定名称时的行为不一致
- javascript - 如何生成令牌?
- inno-setup - 在 TInputDirWizardPage 上浏览文件夹时生成的 Inno Setup 事件?
- flutter - Flutter ListView 在列内崩溃
- sql-server - 目标平台 Azure SQL 数据库不支持 CREATE SEARCH PROPERTY LIST
- jquery - 滚动到联系表 7 (wordpress) 中的成功字段
- php - PHP未设置导致内存耗尽