英文:
IllegalStateException LogicalConnectionManagedImpl is closed Hibernate
问题
我正在使用H2数据库与Java上的Hibernate,但我遇到了一个奇怪的错误。
我已经创建了我的抽象仓库以管理基本的CRUD操作。
我得到的异常信息如下:
java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@d20d74a已关闭
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:135)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:116)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:294)
at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:139)
at repositories.AbstractRepository.save(AbstractRepository.java:32)
at services.ResultService.saveResult(ResultService.java:76)
at services.API.WebRequestService.run(WebRequestService.java:124)
at services.API.ThreadService.run(ThreadService.java:67)
AbstractRepository的保存方法:
public <T> T save(T t) {
Transaction transaction = null;
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
Serializable entityId = session.save(t);
transaction.commit();
T createdEntity = (T) session.get(t.getClass(), entityId);
return createdEntity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
return null;
}
我是一名计算机科学专业的学生,对Hibernate不太熟悉。我在自己的计算机上没有遇到这个错误,只在构建了JAR文件的其他计算机上遇到。
附注:英语不是我的主语言,所以如果你不太明白我的意思,我非常抱歉!
英文:
I'm using H2 Database with hibernate on JAVA and I'm getting a weird error.
I have created my abstract repository to manage the basic CRUD operation.
The exception I am getting is this:
java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@d20d74a is closed
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:135)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:116)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:294)
at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:139)
at repositories.AbstractRepository.save(AbstractRepository.java:32)
at services.ResultService.saveResult(ResultService.java:76)
at services.API.WebRequestService.run(WebRequestService.java:124)
at services.API.ThreadService.run(ThreadService.java:67)
AbstractRepository save method:
public <T> T save(T t) {
Transaction transaction = null;
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
Serializable entityId = session.save(t);
transaction.commit();
T createdEntity = (T) session.get(t.getClass(), entityId);
return createdEntity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
return null;
}
I am a CS student and I am not very much familiar with Hibernate. I'm not getting this error on my computer, only on other computers with the JAR file builded.
P.S English isn't my main language so I am very sorry if you don't understand me clearly!
答案1
得分: 6
经过数小时的调试,我找到了错误!
错误是某一列超出了长度限制,异常是从 catch 块中产生的。
该 catch 块试图回滚某个已经关闭连接的内容。
希望对某人有所帮助!
英文:
After hours of debugging I found the error!
The error was that a column exceeded the length and the exception was coming from the catch block.
The catch block was trying to rollback something that its connection was already closed.
I hope this will be helpful to someone!
答案2
得分: 2
我在尝试使用 Hibernate 版本 5.5.3 在两个表之间创建嵌入式关系时遇到了相同的错误。是的,上面的回答对我在一次调试中解决错误很有帮助。感谢 @William。在我的情况下也是一样的,捕获块试图回滚事务,因为在可嵌入类中出现异常。问题在于在可嵌入类内部我没有一个默认构造函数。
谢谢!
英文:
I got the same error when trying to create an embedded relationship between two tables using Hibernate version 5.5.3. Yes the above answer was helpful for me to debug the error in a single go. Thanks to @William. It was same in my case too, the catch block was trying to rollback the transaction due to an exception occurred in the Embeddable class. The issue was that I did not have a default constructor inside Embeddable class.
Thanks!
答案3
得分: 0
@Vikarm的答案和@William的答案指引我走向了正确的方向。您只需在具有EmbeddedId的类的构造函数中创建您的复合ID。
以下是代码示例(完整示例已包含,以保证完整性)
@Entity(name = "artwork_rating")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ArtworkRating {
@EmbeddedId
private ArtworkRatingKey id; // 这需要被实例化*
private int score;
private String content;
// --------- 关联关系 --------- //
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "user_id")
User user;
@ManyToOne
@MapsId("artworkId")
@JoinColumn(name = "artwork_id")
Artwork artwork;
// --------- 构造函数 --------- //
public ArtworkRating(int score, String content, User user, Artwork artwork) {
this.id = new ArtworkRatingKey(user.getId(), artwork.getId()); // *如此所示
this.score = score;
this.content = content;
this.user = user;
this.artwork = artwork;
}
}
英文:
@Vikarm's answer and @William's answer pointed me to the right direction. You simply need to create your composite ID in the constructor of the class that has the EmbeddedId.
Here's how it looks like in code (full example is included for completeness purposes)
@Entity(name = "artwork_rating")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ArtworkRating {
@EmbeddedId
private ArtworkRatingKey id; // This needs to be instantiated*
private int score;
private String content;
// --------- Relations --------- //
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "user_id")
User user;
@ManyToOne
@MapsId("artworkId")
@JoinColumn(name = "artwork_id")
Artwork artwork;
// --------- Constructors --------- //
public ArtworkRating(int score, String content, User user, Artwork artwork) {
this.id = new ArtworkRatingKey(user.getId(), artwork.getId()); // *as shown here
this.score = score;
this.content = content;
this.user = user;
this.artwork = artwork;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论