I have a really strange problem.I wrote an application which is using spring+hibernate. my hibernate configurations is like below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${hospital_"/>
<property name="location" value="hospital.properties"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${hospital_hibernate.driverClassName}"/>
<property name="jdbcUrl" value="${hospital_hibernate.url}"/>
<property name="user" value="${hospital_hibernate.username}"/>
<property name="password" value="${hospital_hibernate.password}"/>
<!--
<property name="acquireIncrement" value="20"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="acquireRetryDelay" value="1000"/>
<property name="autoCommitOnClose" value="true"/>
<property name="checkoutTimeout" value="10000"/>
<property name="debugUnreturnedConnectionStackTraces" value="true"/>
<property name="idleConnectionTestPeriod" value="100"/>
<property name="initialPoolSize" value="1"/>
<property name="maxConnectionAge" value="1000"/>
<property name="maxConnectionAge" value="1000000"/>
<property name="maxIdleTime" value="200"/>
<property name="maxIdleTimeExcessConnections" value="3600"/>
<property name="maxPoolSize" value="10"/>
<property name="minPoolSize" value="1"/>
<property name="preferredTestQuery" value="select 1"/>
<property name="testConnectionOnCheckin" value="false"/>
<property name="unreturnedConnectionTimeout" value="1000"/>
-->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="schemaUpdate" value="${hospital_schema.update}"/>
<property name="mappingResources">
<list>
<value>com/saman/entity/hbms/Employee.hbm.xml</value>
<value>com/saman/entity/hbms/Shift.hbm.xml</value>
<value>com/saman/entity/hbms/Patient.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hospital_hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hospital_hibernate.showsql}</prop>
<!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
<prop key="hibernate.generate_statistics">true</prop>
<!--<prop key="hibernate.cache.use_second_level_cache">true</prop>-->
<!--<prop key="hibernate.cache.use_query_cache">true</prop>-->
<!--<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>-->
<!--<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>-->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<!--<property name="prepareConnection" value="true"/>-->
<!--<property name="hibernateManagedSession" value="true"/>-->
</bean>
</beans>
and I also set my tx:advice like below:
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionServiceOperation"
expression="execution(* com.saman.svc.dalc.IEmployeeDA.*(..))"/>
<aop:pointcut id="transactionServiceOperation2"
expression="execution(* com.saman.svc.dalc.IPatientDA.*(..))" />
<aop:pointcut id="transactionServiceOperation3"
expression="execution(* com.saman.svc.dalc.IShiftDA.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation2"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation3"/>
</aop:config>
the problem here is that when i run my application, for the 10 or 11 database accesses it works fine, but after that, it seems like that my applications get locked and doesn't respond.
I really don't know where is the problem.
p.s. I define all my DAs in patientDA, shiftDA, and employeeDA classes which inherits IPatientDA, IShiftD, and IEmployeeDA.
my GenericDA:
public class GenericDA<TEntity, TId extends Serializable> extends HibernateDaoSupport implements IGenericDA<TEntity, TId>
{
private Boolean isDateString = false;
private Dialect mappingDialect = Dialect.mysql;
public Boolean isDateString()
{
return isDateString;
}
public void setDateString(Boolean dateString)
{
isDateString = dateString == null ? false : dateString;
}
public Dialect getMappingDialect()
{
return mappingDialect;
}
public Dialect dialect()
{
return this.getMappingDialect();
}
public void setMappingDialect(Dialect mappingDialect)
{
this.mappingDialect = mappingDialect;
}
public Class getType()
{
Type tClass = null;
Type type = this.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType)
{
ParameterizedType paramType = (ParameterizedType) type;
tClass = paramType.getActualTypeArguments()[0];
}
return (Class) tClass;
}
public String getStringType()
{
return this.getType().getName();
}
public TEntity get(TId id)
{
return (TEntity) this.getSession().get(getType(), id);
}
public List<TEntity> get()
{
return (List<TEntity>) this.getSession().createCriteria(getType()).list();
}
public List<TEntity> get(long from, long size)
{
return this.getSession().createCriteria(getType())
.setFirstResult((int) from)
.setMaxResults((int) size)
.list();
}
public TEntity insert(TEntity entity)
{
this.getSession().save(entity);
return entity;
}
public TEntity update(TEntity entity)
{
try
{
this.getSession().evict(entity);
this.getSession().update(entity);
//this.getSession().flush();
}
catch (HibernateException ex)
{
this.getSession().evict(entity);
this.getSession().clear();
// this.getSession().update(entity);
throw ex;
}
return entity;
}
public void delete(TId id)
{
this.getSession().delete(get(id));
}
public void delete(TEntity entity)
{
this.getSession().delete(entity);
}
public List<TEntity> get(int from, int size)
{
//noinspection unchecked
return this.getSession().createCriteria(getType())
.setFirstResult(from)
.setMaxResults(size)
.list();
}
public long getCount()
{
return (Long) this.getSession().createQuery("select count(entity.id) from " + getStringType() + " entity")
.uniqueResult();
}
public Object getProperty(Long id, String property)
{
Criteria criteria = this.getSession().createCriteria(getType());
criteria.add(Restrictions.eq("id", id));
criteria.setProjection(Projections.property(property));
criteria.setMaxResults(1);
return criteria.uniqueResult();
}
public org.hibernate.Session prepareFilteredSession(Date effectiveDate)
{
org.hibernate.Session session = this.getSession();
if (effectiveDate != null)
session.enableFilter("effectiveDate").setParameter("fDate", effectiveDate);
return session;
}
public void DisableFilter(org.hibernate.Session session)
{
session.disableFilter("effectiveDate");
}
public String getLikeExpression(String filter, String parameter)
{
String result = "LIKE";
if (filter.startsWith("*"))
{
if (this.mappingDialect.equals(Dialect.oracle))
result += " '%' ||";
else
result += " '%' +";
}
result += " :" + parameter + " ";
if (filter.endsWith("*"))
{
if (this.mappingDialect.equals(Dialect.oracle))
result += " || '%'";
else
result += " + '%'";
}
return result;
}
public String getLikeParameter(String parameter)
{
if (parameter.startsWith("*"))
parameter = parameter.substring(1);
if (parameter.endsWith("*"))
parameter = parameter.substring(0, parameter.length() - 1);
return parameter;
}
public Query setDateParameter(Query query, String name, Date value)
{
if (this.isDateString)
return query.setString(name, String.format("%1$tY-%1$tm-%1$td %tT", value));
else
return query.setParameter(name, value);
}
}
and for example my ShiftDA example is:
public class ShiftDA extends GenericDA<ShiftEntity, Long> implements IShiftDA {
public void updateNumberOfPatients( int numberOfPatients, Long columnID ){
try{
Statement st = this.getSession().connection().createStatement();
String sql = "UPDATE shift SET CurrentNumberOfPatients='" + numberOfPatients + "' WHERE ShiftId='" + columnID.toString() + "' ";
st.executeUpdate(sql);
}catch( Exception e ){
e.printStackTrace();
}
}
}
Judging by these C3P0 settings (min =1 max = 10) and the 10 or 11 requests you have specified I'd take a guess and say that you are exhausting the connection pool because something isn't releasing them. I suspect this is related to the tx-manager and it not knowing when the session's life is over.
Also - I would use commons DBCP rather than C3P0.
Can you post what the DA objects are doing?
Related
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="org.hibernate.cache.ehcache.configurationResourceName">classpath:hibernate-ehcache.xml</prop>
<prop key="cache.provider_class">${cache.provider_class}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
</bean>
<!-- Hibernate template for hibernateSessionFactory -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Transaction Manager for hibernateSessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="DataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Above is the Hibernate config for the spring boot application. We have addition added the following c3po config. But still the connection pool is getting maxed out.
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property name>
<property name="jdbcUrl" value="${ondot.jdbc.url}"></property name>
<property name="user" value="${jdbc.user}"></property name>
<property name="password" value="${jdbc.password}"></property name>
<property name="initialPoolSize" value="${initialPoolSize}" /></property name>
<property name="minPoolSize" value="${minPoolSize}"></property name>
<property name="maxPoolSize" value="${maxPoolSize}"></property name>
<property name="acquireIncrement" value="${acquireIncrement}"></property name>
<property name="maxStatements" value="${maxStatements}"></property name>
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"></property name>
<property name="acquireRetryDelay" value="${acquireRetryDelay}"> </property name>
<property name="breakAfterAcquireFailure"
value="${breakAfterAcquireFailure}"></property name>
<property name="maxIdleTime" value="${maxIdleTime}"> </property name>
and following is the property file:
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.generate_statistics=true
cache.provider_class=org.hibernate.cache.EhCacheProvider
initialPoolSize=5
minPoolSize=100
maxPoolSize=250
acquireIncrement=5
maxStatements=100
acquireRetryAttempts=10
acquireRetryDelay=1000
breakAfterAcquireFailure=false
maxIdleTime=1800
The session factory is autowired and a simple code with Open and close session is written.
public class BasicAunthenticationImpl {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory)
{
BasicAunthenticationImpl.sessionFactory = sessionFactory;
}
}
public class Module1
{
#Autowired
private SessionFactory sessionFactory;
private Session session = null;
#Override
public void process(List<RoutableModuleData> routableProcessObjects)
throws OndotException
{
try
{
session = sessionFactory.openSession();
session.flush();
canonicalMessage =
lookupprocesser.processInput(canonicalMessage,
Constants.L1_LOOKUP_CACHE, session);
}
catch (Exception Ex)
{
}
finally
{
if (session != null && session.isOpen()){
session.close();
}
}
}
}
public class Lookupprocesser{
public CanonicalMessage processInput(CanonicalMessage canonicalMessage,
String cacheLookup, Session session) throws DataInsightsCommonException
{
lookupSearchForL1(canonicalMessage, session, cacheLookup);
return canonicalMessage;
}
}
public class Query{
public List<TerminalData> searchByMatchKey1AndMatchKey2(Session
session, String matchKey1, String matchKey2) throws
DataInsightsCommonException
{
Query query;
List<Data> t = null;
query = session.createQuery("from Data as odt where odt.Key1 is
null and odt.Key2 = :Key2");
query.setParameter(Constants.KEY2, Key2);
query.setCacheable(true);
terminals = (List<Data>) query.list();
return (null != t && !t.isEmpty()) ? t : null;
}
}
The Session object is shared among all classes and object, I edited the code below , BasicAuth class creates the SessionFactory
The Module1 will call LookupProcessor and which will call the query class. We are closing the session on the Module1.
I have written the below code in Spring for handling custom exceptions. My Exception handler is executing But it returns a view as shown below instead of a String. May I know what is wrong in my code. Thanks in advance.
Code:
#Controller
public class RestController {
#ExceptionHandler(SpringException.class)
public String handleCustomException(SpringException ex) {
String jsonInString = "{}";
JSONObject json = new JSONObject();
Map<String,String> errorMap = new HashMap<String,String>();
errorMap.put("errorMessage",ex.getMessage());
System.out.println("Inside exception handler");
json.putAll(errorMap);
jsonInString = json.toJSONString();
logger.info(jsonInString);
return jsonInString;
}
#RequestMapping(value = "/v1/dist_list/{emailId}/members", method = RequestMethod.GET)
public #ResponseBody String getDistributionListMember(#PathVariable String emailId) throws Exception, SpringException {
String retStatus = null;
retStatus = dataServices.getDistributionListMember(emailId, callerId);
if (!retStatus.isEmpty()) {
if (retStatus.contains("callerid is not valid")) {
throw new SpringException("Error", retStatus);
}
}
}
SpringException class
package com.uniteid.model;
import org.springframework.http.HttpStatus;
public class SpringException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String errCode;
private String errMsg;
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public SpringException(String errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
}
Below is my Spring-config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.uniteid.controller" />
<mvc:annotation-driven
content-negotiation-manager="contentNegociationManager" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:uniteidrest.properties" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url">
<value>${eidms.url}</value>
</property>
<property name="username">
<value>${eidms.username}</value>
</property>
<property name="password">
<value>${eidms.password}</value>
</property>
</bean>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"
/> <property name="url" value="jdbc:oracle:thin:#un.org:1521:EIDMSUAT"
/> <property name="username" value="EIDMSUAT" /> <property name="password"
value="NewPass" /> </bean> -->
<bean id="contentNegociationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/json" />
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.uniteid.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.connection.pool_size">10</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name = "exceptionMappings">
<props>
<prop key = "com.uniteid.model.SpringException">
ExceptionPage
</prop>
</props>
</property>
<property name = "defaultErrorView" value = "error"/>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataDao" class="com.uniteid.dao.DataDaoImpl"></bean>
<bean id="dataServices" class="com.uniteid.services.DataServicesImpl"></bean>
</beans>
In my app I've been passing my SessionFatory in my method parameters when I need to access my database within those methods. It's instantiated in my Controller using:
#Autowired
private SessionFactory sessionFactory;
And when I need to access my database I use lines like this:
sessionFactory.getCurrentSession().save()
// or
List products = sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM PRODUCTS").list();
Should I be creating new sessionFactories to get my current session or should is it okay to pass it as a parameter?
edit [added for clarity]:
From HomeController.java:
#Controller
public class HomeController {
#Autowired
private SessionFactory sessionFactory;
//Correlations Insert Handler
//DOCUMENT CREATE
#RequestMapping(value="/documents", method = RequestMethod.PUT)
public #ResponseBody String insertDocument(HttpServletRequest request, HttpServletResponse response){
DocumentControl documentControl = new DocumentControl();
documentControl.insertDocument(request, response, sessionFactory);
return "went through just find";
}
//DOCUMENT READ
#RequestMapping(value="/documents", method = RequestMethod.GET)
public #ResponseBody List<Documents> getAllDocuments(){
//List documents = sessionFactory.getCurrentSession().createQuery("from Documents").list();
DocumentControl documentControl = new DocumentControl();
List documents = documentControl.getAllDocuments(sessionFactory);
return documents;
}
From DocumentControl.java:
public class DocumentControl {
private static Logger logger = Logger.getLogger(DocumentControl.class.getName());
public DocumentControl(){};
//CREATE
public void insertDocument(HttpServletRequest request, HttpServletResponse response, SessionFactory sessionFactory){
Documents document = new Documents(request)
sessionFactory.getCurrentSession().save(document);
}
//READ
public List<DocumentReturn> getAllDocuments(SessionFactory sessionFactory){
List documents = sessionFactory.getCurrentSession().createQuery("from Documents").list();
return documents;
}
private boolean ifProductExists (String productCode, SessionFactory sessionFactory) {
boolean exists = false;
List<Products> productList = sessionFactory.getCurrentSession().createCriteria(Products.class).add( Restrictions.eq("productCode", productCode)).list();
if ( !productList.isEmpty() && productList.size() > 0 ) {
exists = true;
}
return exists;
}
private boolean ifStandardExists (String standardCode, SessionFactory sessionFactory) {
boolean exists = false;
List<Standards> standardList = sessionFactory.getCurrentSession().createCriteria(Standards.class).add( Restrictions.eq("standardCode", standardCode)).list();
if ( !standardList.isEmpty() && standardList.size() > 0 ) {
exists = true;
}
return exists;
}
}
hibernate.cfg.xml:
hibernate-configuration>
<session-factory>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
<mapping class="***.*****.********.model.Documents"/>
</session-factory>
</hibernate-configuration>
persistence-context.xml:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key=" hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<
property name="sessionFactory" ref="sessionFactory" />
</bean>
SessionFactory is a Singleton. Therefore, you should get it from the ApplicationContext (i.e. using the #Autowired annotation). Passing it as a parameter will only complicate your API.
I think that you should leave creation of SessionFactory to the framework you are using and inject it as a resource just as you are doing by now. Besides you are not creating it in your code at all. Good practice is to create separate session for every chunk of data manipulation, if it is not done by a framework ofc. Session should be your main unit of work.
The resolvers customizeFault method isnt being hit when I set the mapped endpoints below. If I remove it, it gets hit. Any help? The exception is thrown in my resolver class.
MembershipManagementEndpoint.java
package com.openclass.adapter.ws;
#Endpoint
public class MembershipManagementEndpoint {
Spring Config
<bean id="membershipExceptionResolver" class="com.openclass.adapter.ws.resolvers.LisMembershipResolverInterceptor">
<property name="order" value="1"></property>
<property name="mappedEndpoints">
<set>
<value>com.openclass.adapter.ws.MembershipManagementEndpoint</value>
</set>
</property>
</bean>
LisSoapFaultTranslatorExceptionResolver.java
package com.openclass.adapter.ws.resolvers;
public class LisMembershipResolverInterceptor extends LisSoapFaultTranslatorExceptionResolver {
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
throws Exception {
throw new LisMembershipException();
return true;
}
LisSoapFaultTranslatorExceptionResolver.java
public abstract class LisSoapFaultTranslatorExceptionResolver extends
SimpleSoapExceptionResolver implements SoapEndpointInterceptor {
I add to build separate interceptor/resolver for each endpoint and then test in the interceptor/resolver and test for the correct endpoint.
SoapFaultExceptionResolver
#Override
protected final boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
if(methodEndpoint != null && methodEndpoint.getBean().getClass().getSimpleName().equalsIgnoreCase(getEndpointClass().getSimpleName()))
{
Assert.isInstanceOf(SoapMessage.class, messageContext.getResponse(),
"SimpleSoapExceptionResolver requires a SoapMessage");
SoapMessage response = (SoapMessage) messageContext.getResponse();
String faultString = StringUtils.hasLength(ex.getMessage()) ? ex.getMessage() : ex.toString();
SoapBody body = response.getSoapBody();
SoapFault fault = body.addServerOrReceiverFault(faultString, getLocale());
customizeFault(messageContext, endpoint, ex, fault);
return true;
}
return false;
}
Config
<bean id="courseSectionExceptionResolver" class="com.test.adapter.ws.resolvers.LisCourseSectionResolverInterceptor">
<property name="order" value="1"></property>
</bean>
<bean id="personExceptionResolver" class="com.test.adapter.ws.resolvers.LisPersonResolverInterceptor">
<property name="order" value="2"></property>
</bean>
<bean id="membershipExceptionResolver" class="com.test.adapter.ws.resolvers.LisMembershipResolverInterceptor">
<property name="order" value="3"></property>
</bean>
<bean id="bdemsExceptionResolver" class="com.test.adapter.ws.resolvers.LisBdemsResolverInterceptor">
<property name="order" value="4"></property>
</bean>
<bean id="outcomeExceptionResolver" class="com.test.adapter.ws.resolvers.LisOutcomeResolverInterceptor">
<property name="order" value="5"></property>
</bean>
<sws:interceptors>
<sws:payloadRoot namespaceUri="http://www.imsglobal.org/services/lis/cmsv1p0/wsdl11/sync/imscms_v1p0">
<bean class="com.test.adapter.ws.resolvers.LisCourseSectionResolverInterceptor"/>
</sws:payloadRoot>
<sws:payloadRoot namespaceUri="http://www.imsglobal.org/services/lis/pms2p0/wsdl11/sync/imspms_v2p0">
<bean class="com.test.adapter.ws.resolvers.LisPersonResolverInterceptor"/>
</sws:payloadRoot>
<sws:payloadRoot namespaceUri="http://www.imsglobal.org/services/lis/mms2p0/wsdl11/sync/imsmms_v2p0">
<bean class="com.test.adapter.ws.resolvers.LisMembershipResolverInterceptor"/>
</sws:payloadRoot>
<sws:payloadRoot namespaceUri="http://www.imsglobal.org/services/lis/bdems1p0/wsdl11/sync/imsbdems_v1p0">
<bean class="com.test.adapter.ws.resolvers.LisBdemsResolverInterceptor"/>
</sws:payloadRoot>
<sws:payloadRoot namespaceUri="http://www.imsglobal.org/services/lis/oms1p0/wsdl11/sync/imsoms_v1p0">
<bean class="com.test.adapter.ws.resolvers.LisOutcomeResolverInterceptor"/>
</sws:payloadRoot>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
<property name="logRequest" value="true"/>
<property name="logResponse" value="true"/>
</bean>
</sws:interceptors>
I have a Using c3p0 0.9.1.2 , hibernate 3.2.1.ga and spring 2.5.5. The problem is the database connection doesn't close itself. Here are the logs :
[22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] acquire test -- pool is already maxed out. [managed: 20; max: 20]
[22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection#15cc604
[22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] trace com.mchange.v2.resourcepool.BasicResourcePool#6b0524 [managed: 20, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection#15cc604)
Here's the datasource configuration :
<!-- Local DataSource that works in any environment -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${database.driver}"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.user}"/>
<property name="password" value="${database.password}"/>
<!--<property name="connectionCustomizerClassName" value="org.xxx.webapp.common.persistence.WatchConnectionCustomizer"/>-->
<property name="maxStatements" value="500"/>
<property name="maxIdleTime" value="1800"/>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="2"/>
<property name="initialPoolSize" value="2"/>
<property name="acquireIncrement" value="3"/>
<property name="idleConnectionTestPeriod" value="3000"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
<prop key="hibernate.transaction.auto_close_session">${hibernate.transaction.auto_close_session}</prop>
<prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
Here's our generic Dao
public class GenericDAO<T, PK extends Serializable> extends HibernateDaoSupport
implements IGenericDAO<T, PK> {
private Class<T> clazz;
private Logger logger = Logger.getLogger(GenericDAO.class);
private static Session session;
public GenericDAO(Class<T> clazz) {
this.clazz = clazz;
}
public void refresh(T instanceToRefresh) throws DataAccessException {
getHibernateTemplate().refresh(instanceToRefresh);
//getCurrentSession().refresh(instanceToRefresh);
}
public void saveOrUpdate(T instanceToSaveOrUpdate)
throws DataAccessException {
//getCurrentSession().saveOrUpdate(instanceToSaveOrUpdate);
getHibernateTemplate().saveOrUpdate(instanceToSaveOrUpdate);
}
public void persist(T instanceToPersist) throws DataAccessException {
getHibernateTemplate().persist(instanceToPersist);
//getCurrentSession().persist(instanceToPersist);
}
#SuppressWarnings("unchecked")
public T merge(T instanceToMerge) throws DataAccessException {
T instanceMerged = (T) getHibernateTemplate().merge(instanceToMerge);
//T instanceMerged = (T) getCurrentSession().merge(instanceToMerge);
return instanceMerged;
}
#SuppressWarnings("unchecked")
public PK save(T newInstance) throws DataAccessException {
return (PK) getHibernateTemplate().save(newInstance);
//return (PK) getCurrentSession().save(newInstance);
}
public void delete(T persistentObject) throws DataAccessException {
getHibernateTemplate().delete(persistentObject);
//getCurrentSession().delete(persistentObject);
}
#SuppressWarnings("unchecked")
public T load(PK id) {
return (T) getHibernateTemplate().get(clazz, id);
//return (T) getCurrentSession().get(clazz, id);
}
public void update(T transientObject) throws DataAccessException {
//getCurrentSession().update(transientObject);
getHibernateTemplate().update(transientObject);
}
#SuppressWarnings("unchecked")
public List<T> loadAll() throws DataAccessException {
//Session session = this.getCurrentSession();
//return session.createQuery("from " + clazz.getName()).list();
return getHibernateTemplate().loadAll(clazz);
}
}
Thanks in advance.
Normally, the connection is automatically closed by hibernate. However, a few things to note:
long-running transactions may occupy a connection
improper session management may mean you don't close your session, which in turn means the connection remains in use
The typical setup when using spring is to annotate your service methods with #Transactional. That way spring will manage your transactions and sessions.
We have used the following line in the dispatcher-servlet.xml file and database physical connections are now closing.
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>