title: Could not open Hibernate Session for transaction / Cannot open connection
body: <p>In my application, I have a module that is designed to search a database for users and display their information in a table inside a jsp. I just setup Spring Security inside my application. I was able to make a connection to the database from the login page, though for some reason none of my DAO's CRUD operations (in this case, the search) are working.</p>
  
  <p>Thanks, and please let me know if I can provide any more information.</p>
  
  <p><strong>Here is most of my stack trace.</strong></p>
  
  <pre><code>    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:656)
          org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  
  ...
  
  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:596)
      org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
      org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
      org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      $Proxy8.searchEntity(Unknown Source)
      arlua.controller.SearchUserController.mySearchMethod(SearchUserController.java:35)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source
  
  ...
  
      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.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:74)
          $Proxy14.getTransactionIsolation(Unknown Source)
  
  ...
  
      java.sql.SQLException: invalid arguments in call
          oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
          oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
          oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
          oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
          oracle.jdbc.driver.PhysicalConnection.&lt;init&gt;(PhysicalConnection.java:414)
          oracle.jdbc.driver.T4CConnection.&lt;init&gt;(T4CConnection.java:165)
          oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
          oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
          java.sql.DriverManager.getConnection(Unknown Source)
          java.sql.DriverManager.getConnection(Unknown Source)
          org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
          org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
          org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
          org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
          org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
          org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
          org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
          org.hibernate.jdbc.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:74)
          $Proxy14.getTransactionIsolation(Unknown Source)
          org.springframework.jdbc.datasource.DataSourceUtils.prepareConnectionForTransaction(DataSourceUtils.java:190)
          org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:508)
          org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
          org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
          org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
          org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
          org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
          org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
          org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
          $Proxy8.searchEntity(Unknown Source)
          arlua.controller.SearchUserController.mySearchMethod(SearchUserController.java:35)
          sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          java.lang.reflect.Method.invoke(Unknown Source)
          org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
          org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
          org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
          org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
          org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
          org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
          org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
          org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
          org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
          org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
          org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
          org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
          org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
  </code></pre>
  
  <p><strong>SearchUserController class</strong></p>
  
  <pre><code>package arlua.controller;
  
  import java.util.List;
  
  import arlua.dao.TableEntityFetchDao;
  import arlua.exception.ImproperUserSearchException;
  import arlua.exception.SearchStringTooShortException;
  import arlua.helper.BuildUserTables;
  import arlua.service.SearchCriteria;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.SessionAttributes;
  import org.springframework.web.servlet.ModelAndView;
  
  @Controller
  @SessionAttributes
  public class SearchUserController {
  
      private @Autowired @Qualifier("userInfoDaoImpl") TableEntityFetchDao userInfoDao;
  
      List&lt;?&gt; searchList;
      String error;
  
      @RequestMapping(value = "/search_user", method = RequestMethod.POST)
      public String mySearchMethod(@ModelAttribute("search_criteria") SearchCriteria search){
  
          error = "";
          try{
              if(search.getInput() != null)
                  searchList = userInfoDao.searchEntity(search.getInput());
          }
          catch(SearchStringTooShortException e1){
              error = "Search criteria is too short. Be more specific.";
          }
          catch(ImproperUserSearchException e2){
              error = "Specify either an Id or first and last name.";
          }
  
          return "redirect:search_user";
      }
  
      @RequestMapping("/search_user")
      public ModelAndView mySuccessMethod(){
          ModelAndView model = new ModelAndView("search_user");
  
          if(searchList != null){
              if(searchList.size() == 0)
                  error = "Search returned no results.";
              else
                  model.addObject("searchTable", BuildUserTables.buildSearch(searchList));
          }
  
          return model;
      }
  
  }
  </code></pre>
  
  <p><strong>UserInfoDaoImpl class</strong></p>
  
  <pre><code>package arlua.dao.impl;
  
  import arlua.dao.TableEntityFetchDao;
  import arlua.exception.ImproperUserSearchException;
  import arlua.exception.SearchStringTooShortException;
  import arlua.tables.UserInfoTable;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  
  public class UserInfoDaoImpl implements TableEntityFetchDao{
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sessionFactory){
          this.sessionFactory = sessionFactory;
      }
  
      public void saveEntity(Object userInfo) {
          this.sessionFactory.getCurrentSession().save((UserInfoTable)userInfo);
      }
  
      public UserInfoTable getEntity(Object user_id) {
          return (UserInfoTable)this.sessionFactory.getCurrentSession().
              get(UserInfoTable.class, (String)user_id);
      }
  
      public void updateEntity(Object userInfo) {
          this.sessionFactory.getCurrentSession().update((UserInfoTable)userInfo);
      }
  
      public void deleteEntity(Object userInfo) {
          this.sessionFactory.getCurrentSession().delete((UserInfoTable)userInfo);
      }
  
      public List&lt;?&gt; getAllEntities() {
          return this.sessionFactory.getCurrentSession().createQuery
              ("FROM UserInfoTable").list();
      }
  
      public List&lt;?&gt; searchEntity(String search) throws SearchStringTooShortException, ImproperUserSearchException{
          //This search supports input of a user's seid OR a first and last name.
          if(search.length() &lt; 3){
              throw new SearchStringTooShortException();
          }
  
          Session s = this.sessionFactory.getCurrentSession();
          List&lt;?&gt; searchList = new ArrayList();
  
          //Searches for similar users with matching SEIDs.
          if(search.matches("^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$")){
              searchList = s.createQuery
                  //("FROM UserInfoTable").list();
                  ("FROM UserInfoTable WHERE UPPER(user_id) LIKE UPPER('%" + search + "%')").list();
  
          }
          //Searches for similar users with a matching first and last name.
          //If the search contains more than 2 arguments, an exception will be thrown.
          else if(search.length() &gt;= 3){
              String[] searchParts = search.split(" ");
              if(searchParts.length != 2)
                  throw new ImproperUserSearchException();
              searchList = s.createQuery
                  ("FROM UserInfoTable WHERE UPPER(first_name) LIKE UPPER('%" + searchParts[0] + "%') " +
                          "AND UPPER(last_name) LIKE UPPER('%" + searchParts[1] + "%')").list();
          }
          else{
              throw new ImproperUserSearchException();
          }
  
          return searchList;
      }
  }
  </code></pre>
  
  <p><strong>application-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;bean id="serverDatasource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName"&gt;
              &lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;url&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter"&gt;
          &lt;property name="targetDataSource" ref="serverDatasource"/&gt;
          &lt;property name="username"&gt;&lt;value&gt;unknown&lt;/value&gt;&lt;/property&gt;
          &lt;property name="password"&gt;&lt;value&gt;unknown&lt;/value&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!--
      &lt;bean id="propertyPlaceholderConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt;
          &lt;property name="ignoreUnresolvablePlaceholders" value="true"/&gt;
      &lt;/bean&gt;
      --&gt;
  
      &lt;bean id="mySessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="mappingLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;WEB-INF/mapping/user_info.hbm.xml&lt;/value&gt;
                  &lt;value&gt;WEB-INF/mapping/login.hbm.xml&lt;/value&gt;
                  &lt;value&gt;WEB-INF/mapping/linked_accounts.hbm.xml&lt;/value&gt;
                  &lt;value&gt;WEB-INF/mapping/application_instance.hbm.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="dataSource" ref="serverDatasource"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "userInfoDaoImpl" class="arlua.dao.impl.UserInfoDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "loginDaoImpl" class="arlua.dao.impl.LoginDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "linkedAccountsDaoImpl" class="arlua.dao.impl.LinkedAccountsDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "applicationInstanceDaoImpl" class="arlua.dao.impl.ApplicationInstanceDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "roleDaoImpl" class="arlua.dao.impl.RoleDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "applicationApprovalDaoImpl" class="arlua.dao.impl.ApplicationApprovalDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id = "userDaoImpl" class="arlua.dao.impl.UserDaoImpl"/&gt;
  
      &lt;!--  *************  TRANSACTION MANAGEMENT USING AOP **************** --&gt;
  
      &lt;bean id="myTransactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;    
      &lt;/bean&gt;
  
      &lt;aop:config&gt;
          &lt;aop:pointcut id="allMethods" expression="execution(* arlua.dao.TableEntityFetchDao.*(..))"/&gt;
          &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/&gt;
      &lt;/aop:config&gt;
  
      &lt;tx:advice id="txAdvice" transaction-manager="myTransactionManager"&gt;
          &lt;tx:attributes&gt;
              &lt;tx:method name="saveEntity"
                  propagation = "REQUIRES_NEW"
                  isolation = "READ_COMMITTED"
                  rollback-for = "Exception"/&gt;
              &lt;tx:method name="updateEntity"
                  propagation = "REQUIRES_NEW"
                  isolation = "READ_COMMITTED"
                  rollback-for = "Exception"/&gt;
              &lt;tx:method name="getEntity"
                  propagation = "REQUIRES_NEW"
                  isolation = "READ_COMMITTED"
                  rollback-for = "Exception"/&gt;
              &lt;tx:method name="getAllEntities"
                  propagation = "REQUIRES_NEW"
                  isolation = "READ_COMMITTED"
                  rollback-for = "Exception"/&gt;
              &lt;tx:method name="searchEntity"
                  propagation = "REQUIRES_NEW"
                  isolation = "READ_COMMITTED"
                  rollback-for = "Exception"/&gt;
          &lt;/tx:attributes&gt;
      &lt;/tx:advice&gt;
  
  &lt;/beans&gt;
  </code></pre>