我已经阅读了各种Stackoverflow的问题和网上关于类似问题的内容。然而,我找不到有用的提示来帮助我缩小我的问题范围。以下是导致此错误的使用案例。
两个实体Campus和Programs-->从Campus到Program的一对多关系,从Program到Campus的一对一关系。我正在尝试创建与校园相关联的多个程序。每次插入都将创建一个具有相同详细信息并将其附加到不同(唯一)校园的新程序。例如,在纽约,旧金山,达拉斯,芝加哥提供Java 101课程将为每个校园创建一个新程序。单个程序没有持久化问题。对于最多46个校园的程序也没有问题,但是在第47个校园出现了这个错误。
以下是我的应用程序的配置:
属性文件:
以下是翻译后的结果:
这是 Service 中的一段代码。
这是DAO的一小段代码示例:
两个实体Campus和Programs-->从Campus到Program的一对多关系,从Program到Campus的一对一关系。我正在尝试创建与校园相关联的多个程序。每次插入都将创建一个具有相同详细信息并将其附加到不同(唯一)校园的新程序。例如,在纽约,旧金山,达拉斯,芝加哥提供Java 101课程将为每个校园创建一个新程序。单个程序没有持久化问题。对于最多46个校园的程序也没有问题,但是在第47个校园出现了这个错误。
以下是我的应用程序的配置:
属性文件:
datasource.classname=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/edu
datasource.username=xxx
datasource.password=xxx123
datasource.initialsize=15
datasource.maxactive=50
datasource.maxidle=15
datasource.minidle=5
datasource.maxwait=10000
datasource.dialect=org.hibernate.dialect.MySQLDialect
datasource.validationquery =select 1
datasource.minEvictableIdleTimeMillis = 180000
datasource.timeBetweenEvictionRunsMillis = 180000
hibernate.batchsize=30
这是我的 spring-hibernate 配置如何看待:
<context:property-placeholder location="classpath:database.properties" order="1" ignore-unresolvable="true" />
<context:property-placeholder location="classpath:app.properties" order="2" ignore-unresolvable="true" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${datasource.classname}" />
<property name="url" value="${datasource.url}" />
<property name="username" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
<property name="initialSize" value="${datasource.initialsize}" />
<property name="maxActive" value="${datasource.maxactive}" />
<property name="maxIdle" value="${datasource.maxidle}" />
<property name="minIdle" value="${datasource.minidle}" />
<property name="maxWait" value="${datasource.maxwait}" />
<property name="minEvictableIdleTimeMillis" value="${datasource.minEvictableIdleTimeMillis}" />
<property name="timeBetweenEvictionRunsMillis" value="${datasource.timeBetweenEvictionRunsMillis}" />
<property name="validationQuery" value="${datasource.validationquery}" />
<property name="testOnBorrow" value="true" />
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${datasource.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.batch_size">${hibernate.batchsize}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache-entity.xml</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.generate_statistics">false</prop>
<!-- <prop key="hibernate.connection.release_mode">auto</prop> -->
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.edapp.core</value>
<value>com.edapp.data.engine</value>
<value>com.edapp.service.engine</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config proxy-target-class="true"/> <!-- necessary to call methods on classes than proxies -->
<context:annotation-config />
<context:component-scan base-package="com.edapp" />
<!-- transaction settings -->
<tx:annotation-driven transaction-manager="transactionManager" />
以下是翻译后的结果:
这是我的应用程序流程:
ProgramController -> ProgramService -> ProgramDAO
Service类使用以下注释: @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
DAO类使用以下注释: @Transactional(propagation=Propagation.MANDATORY, isolation=Isolation.READ_COMMITTED)
以下是Controller中的代码片段:
List<String> campuses = Arrays.asList(gson.fromJson(campusesJSArray, String[].class));
if(campuses.size() > 0){
List<Program> programList = new ArrayList<Program>();
AreaOfStudy aos = this.areaOfStudyService.getById(areaOfStudyId);
Concentration con = this.concentrationService.getById(concentrationId);
for(String c : campuses){
Long campusid = Long.parseLong(c);
Program p = new Program();
Campus campus = this.campusService.getById(campusid);
if(campus != null){
System.out.println(campus.toString());
p.setName(name);
p.setCampus(campus);
p.setCode(code);
p.setLevel(level);
p.getCampus().getPrograms().add(p);
p.setAreaOfStudy(aos);
p.setConcentration(con);
p.setActive(true);
}
programList.add(p);
}
((ProgramServiceImpl)programService).saveOrUpdate(programList);
这是 Service 中的一段代码。
if(programList == null){
log.error("ProgramList cannot be null");
return null;
}
Map<Integer, String> errors = new HashMap<Integer, String>();
log.info("Saving program list of size:"+programList.size());
for(int i=0; i<programList.size();i++){
try{
this.saveOrUpdate(programList.get(i));
}catch(HibernateException e){
errors.put(i, "error");
}
}
return errors;
这是DAO的一小段代码示例:
@Transactional(isolation=Isolation.REPEATABLE_READ)
public void create(final T entity) {
if(entity == null){
IllegalArgumentException e = new IllegalArgumentException();
throw(e);
}
Session session = this.sessionFactory.getCurrentSession();
try{
session.persist(entity);
session.flush();
}catch(ConstraintViolationException cve){
log.error("School with same code already exists "+ this.clazz.getName(),cve);
throw cve;
}catch(HibernateException e){
log.error("Error persisting entity of type "+ this.clazz.getName(),e);
throw new HibernateException(e);
}finally{
session.clear();
}
}
批量大小为30
@Transactional(isolation=Isolation.REPEATABLE_READ)
public void create(List<T> entityList){
if(entityList == null){
IllegalArgumentException e = new IllegalArgumentException();
throw(e);
}
Session session = this.sessionFactory.getCurrentSession();
try{
for(int i=0;i<entityList.size();i++){
T entity = entityList.get(i);
if(entity == null){
log.error("List "+ this.clazz.getName() + " of cannot contain null");
throw new NullPointerException("List "+ this.clazz.getName() + " of cannot contain null");
}
session.persist(entity);
if(i% this.batchSize == 0){
session.flush();
session.clear();
}
}
}catch(HibernateException e){
log.error("Error persisting entity of type "+ this.clazz.getName(),e);
throw new HibernateException(e);
}finally{
session.flush();
session.clear();
}
}
尝试使用两种方法进行持久化,但结果相同。
以下是完整的堆栈跟踪
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:932)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:597)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:329)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.tr.leadgen.service.engine.CampusServiceImpl$$EnhancerByCGLIB$$68d579ad.getById(<generated>)
com.tr.leadgen.web.edu.controllers.ProgramController.add(ProgramController.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.hibernate.exception.GenericJDBCException: Cannot open connection
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:556)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:329)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.tr.leadgen.service.engine.CampusServiceImpl$$EnhancerByCGLIB$$68d579ad.getById(<generated>)
com.tr.leadgen.web.edu.controllers.ProgramController.add(ProgramController.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:114)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:83)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:556)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:329)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.tr.leadgen.service.engine.CampusServiceImpl$$EnhancerByCGLIB$$68d579ad.getById(<generated>)
com.tr.leadgen.web.edu.controllers.ProgramController.add(ProgramController.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
java.util.NoSuchElementException: Timeout waiting for idle object
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1174)
org.apache.commons.dbcp.AbandonedObjectPool.borrowObject(AbandonedObjectPool.java:79)
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:83)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:556)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:329)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.tr.leadgen.service.engine.CampusServiceImpl$$EnhancerByCGLIB$$68d579ad.getById(<generated>)
com.tr.leadgen.web.edu.controllers.ProgramController.add(ProgramController.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
这是我从Hibernate获取session的唯一方法:
@Transactional(propagation=Propagation.NOT_SUPPORTED)
protected Session getCurrentSession(){
if(this.sessionFactory == null){
log.error("SessionFactory is null");
}
return this.sessionFactory.getCurrentSession();
}
根据我的应用程序日志,代码执行没有到达服务层。在控制器循环以填充节目实体列表时出现错误(请参阅控制器片段)。
如果有人可以指导我正确的方向,我将不胜感激。