我正在使用JPA的獨立應用程序使用JPA進行對象持久化,而提供程序是Hibernate。現在,當我堅持一個實體並調用EntityTransaction的commit()時,實體被持久化到數據庫。如果在約束等數據庫錯誤有一個例外,當我嘗試回滾我得到一個java.lang.IllegalStateException。什麼都沒有承諾。EntityTransaction永不回滾,引發異常說事務處於未激活狀態
我有一個角色實體,它使用表生成策略爲此實體生成主鍵。無論何時提交失敗,並且下次我保存一個角色時,爲以前的保存生成的序列都會丟失,並且這次是遞增的。這個問題仍然存在於自動增量策略中。在使用hibernate SessionFactory的時候,我沒有遇到這個問題。
代碼:
Role.java
@Entity
@Table(schema="System")
public class Role extends PrincipalEntityBase{
@TableGenerator(schema="System",table="MasterSequence", valueColumnName="Sequence",
pkColumnName="GenKey",pkColumnValue="Role_ID",
name="System.Role", allocationSize=0)
@Id
@GeneratedValue(generator="System.Role",strategy=GenerationType.TABLE)
@Column(name="Role_ID")
private Long role_ID;
@Column(name="RoleName")
private String roleName;
public Role() {
}
public Long getRole_ID() {
return this.role_ID;
}
public void setRole_ID(Long role_ID) {
this.role_ID = role_ID;
}
public String getRoleName() {
return this.roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
Main.java
public static void main(String[] args){
Role r = new Role();
r.setRoleName("Hello");
EntityManager em = persistence.createEntityManagerFactory("test")
.createEntityManager();
EntityTransaction tr = em.getTransaction();
try {
tr.begin();
em.persist(r);
tr.commit();
} catch (Exception e) {
e.printStackTrace();
tr.rollback();
System.out.println("Rolled back");
}
}
堆棧跟蹤:
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
at com.mis.jpa.test.main.Main.main(Main.java:32)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.mis.entity.system.Role]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.mis.entity.system.Role]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
Exception in thread "main" java.lang.IllegalStateException: Transaction not active
at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:104)
at com.mis.jpa.test.main.Main.main(Main.java:35)
DB服務器是MSSQL服務器。
的persistence.xml
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mis.entity.system.Role</class>
<properties>
<!-- Connection properties -->
<property name="javax.persistence.jdbc.driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://192.168.1.2:1433;databaseName=jparesearch" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="admin" />
<!-- JPA Provider Settings -->
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
非常感謝。如果序列上的回滾過度發生,那麼就是正確的,那麼就會出現性能問題,正如你所解釋的那樣。我從來沒有想過這方面。再次感謝。 – Megna