我在Tomcat v2.2.0 EC2服务器上运行了Spring 4.2.4 / Hibernate 5.0.6。
我遇到的问题是在运行服务器一段时间后(超过一天)我偶尔会遇到以下异常
org.hibernate.AssertionFailure: null id in com.app.core.models.User entry (don't flush the Session after an exception occurs)
org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:60)
org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:175)
org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:135)
org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)
org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)
org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1251)
org.hibernate.internal.SessionImpl.list(SessionImpl.java:1767)
org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
com.app.core.dao.AbstractDao.queryBy(AbstractDao.java:57)
com.app.business.MainController.surveryPage(MainController.java:41)
sun.reflect.GeneratedMethodAccessor65.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:178)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
对象 Survery 对象具有User类型的owner字段,因为异常表明id为null,检查Datebase,id不是null。实际上重新启动服务器会使错误消失一段时间。
@Entity
@Table(indexes={@Index(columnList = "OWNER_ID", name = "OWNER_ID_INDEX")})
public class Survey extends Persistable{
...
@JsonIgnore
@ManyToOne
@JoinColumn(name = "OWNER_ID", nullable = true)
private User owner;
...
}
我正在进行的查询操作是:
public List<T> queryBy(String field, Object val) {
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.eq(field, val));
@SuppressWarnings("unchecked")
List<T> l = criteria.list();
transaction.commit();
return l;
}
实际上我担心如果我在会话配置中做错了什么,hibernate的配置如下:
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.reflections.Reflections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.app.core.db.PersistableInterceptor;
import com.app.core.db.Persistable;
@Configuration
@EnableTransactionManagement
@Profile("dev")
public class Config {
.....
@Bean
public HibernateTemplate hibernateTemplate() {
return new HibernateTemplate(sessionFactory());
}
@Bean
public SessionFactory sessionFactory() {
if (sessionFactory!= null){
return sessionFactory;
}
Properties p = new Properties();
p.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
p.put("connection_pool_size", "10");
p.put("hibernate.current_session_context_class", "thread");
p.put("show_sql", "true");
p.put("hibernate.hbm2ddl.auto", "update");
p.put("hibernate.default_schema", "app");
p.put("hibernate.jdbc.batch_size", BATCH_SIZE);
SessionFactory sessionFactory = new LocalSessionFactoryBuilder(getDataSource())
.addAnnotatedClasses(ANNOTATED_CLASSES)
.addProperties(p)
.setInterceptor(new PersistableInterceptor())
.buildSessionFactory();
Config.sessionFactory = sessionFactory;
return sessionFactory;
}
@Bean
public HibernateTransactionManager hibTransMan(){
return new HibernateTransactionManager(sessionFactory());
}
.....
}
有任何想法或建议吗?
答案 0 :(得分:1)
您正在将Hibernate托管事务与Spring托管事务混合使用。当您使用Spring事务管理抽象时,您不必使用特定于资源的事务
您通过@EnableTransactionManagenent启用了Spring事务。如果您的bean有一个@Transactional注释,那么将您的代码重构为
public List<T> queryBy(String field, Object val) {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.eq(field, val));
@SuppressWarnings("unchecked")
List<T> l = criteria.list();
return l;
}
还要更改SessionFactory bean定义以使用Spring提供的LocalSessionFactoryBean。这为使用Spring抽象的事务管理提供了钩子。实施例
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "package.to.your.entity.classes" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource(){
//define datasource
}
private Properties hibernateProperties() {
//hibernate props
}