java – apache commons dbcp connection pool error:使用Spring Tr
|
我已经在网上阅读了类似问题的各种Stackover流程问题和内容.但是,我找不到有用的提示,可以让我缩小我的问题范围.这是我的用例导致此错误.
这是我的应用程序的配置: 属性文件: 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 服务类注释为:@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();
}
}
Batchsize = 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();
}
}
尝试使用这两种方法来坚持但结果相同. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java-给定2d整数数组,递归找到总和为给定数字的路径
- java – 在Map中为术语添加值
- java – Spring Boot Security不会抛出401 Unauthorized Ex
- Java 使用NIO进行快速的文件拷贝的代码
- JAVA JNI函数的注册过程详细介绍
- JBoss 5.1.0 EAP.随机生成java.lang.NoClassDefFoundError
- JSP自定义分页标签TAG全过程
- 解决springMVC 跳转js css图片等静态资源无法加载的问题
- java – 基于Spring Annotation的控制器在jar文件中不起作用
- Java统计字符串中汉字,英文,数字,特殊符号个数
