title: Spring could not obtain transaction-synchronized Session for current thread
body: <p>I am trying to test a change password functionality in my spring application but i got an exception.</p>
  
  <p>I have read similar problems here and tried to use them but they didn't help me. The problem occurs when I want to run the test class to test whether the a <strong>method for changing the password</strong> is working correctly. Here is my test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = UiApplication.class)
  @WebAppConfiguration
  public class SecurityServiceTest {
  
      @Autowired
      SecurityService securityService;
  
      @Test
      public void testLoginSuccess() {
          securityService.login("user1", "pass1");
      }
  
      @Test(expected = BadCredentialsException.class)
      public void testLoginFail() {
          securityService.login("user1", "pass11");
      }
      @Test
      public void testChangePwd(){
  
          securityService.changePwd("user1", "pass1", "123", "123");
          securityService.login("user1", "123");
      }
  }
  </code></pre>
  
  <p>The problem occured when I added the last test - <code>testChangePwd()</code>, before adding it was working properly.</p>
  
  <p>UPDATE- the SecurityService is an interface and its implementation is the following class:</p>
  
  <pre><code>@Service
  public class SecurityServiceImpl implements SecurityService {
  
      private static Logger logger = Logger.getLogger(SecurityServiceImpl.class);
      @Autowired
      private AuthenticationManager authenticationManager;
      @Autowired
      private UserDetailsService userDetailsService;
      @Autowired
      private UserDaoImpl userDao;
      @Override
      public void login(String username, String password) {
  
          UserDetails userDetails = userDetailsService.loadUserByUsername(username);
          UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
  
          authenticationManager.authenticate(usernamePasswordAuthenticationToken);
  
          if (usernamePasswordAuthenticationToken.isAuthenticated()) {
              SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
              logger.info(String.format("Auto login %s successfully!", username));
          }
      }
  
      @Override
      public void changePwd(String username, String password, String newPwd, String againPwd) {
           UserDetails userDetails = userDetailsService.loadUserByUsername(username);
              UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
  
              authenticationManager.authenticate(usernamePasswordAuthenticationToken);
  
              if (usernamePasswordAuthenticationToken.isAuthenticated()) {
                  if(newPwd.equals(againPwd))
                  userDao.findByUserName(username).setPassword(password);
              }
  
      }
  
  }
  </code></pre>
  
  <p>Here is the exception:</p>
  
  <pre><code>org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
      at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
      at com.example.dao.impl.UserDaoImpl.findByUserName(UserDaoImpl.java:26)
      at com.example.dao.impl.UserDaoImpl$$FastClassBySpringCGLIB$$94db05c6.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
      at com.example.dao.impl.UserDaoImpl$$EnhancerBySpringCGLIB$$59fce477.findByUserName(&lt;generated&gt;)
      at com.example.auth.impl.SecurityServiceImpl.changePwd(SecurityServiceImpl.java:50)
      at com.example.auth.SecurityServiceTest.testChangePwd(SecurityServiceTest.java:31)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>