title: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
body: <p>I'm new to SSH, and I'm using Spring 4, Hibernate 4 and Struts 2.
  When I create a unit testing for a DAO class, I got this exception.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath*:testApplicationContext.xml"})
  public class UserDaoTest {
      @Test
      public void testAddUserNotExists() {
          User user = new User("20116524", "785ee107c11dfe36de668b1ae7baacbb");
          userDao.addUser(user);
  
          assertNotNull(userDao.getUserByUsername("20116524"));
      }
  
      @Autowired
      public void setUserDao(UserDao userDao) {
          this.userDao = userDao;
      }
  
      /**
       * UserDao object to test.
       */
      private UserDao userDao;
  }
  </code></pre>
  
  <p>I tried to add following lines to <code>web.xml</code>, but it doesn't work.</p>
  
  <blockquote>
    <p>Reference: <a href="https://stackoverflow.com/questions/6810158/java-hibernate-write-operations-are-not-allowed-in-read-only-mode">Java / Hibernate - Write operations are not allowed in read-only mode</a></p>
  </blockquote>
  
  <p>I believe there's something wrong with my <code>applicationContext.xml</code>, and here's the content:</p>
  
  <pre><code>&lt;!-- Session Factory --&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation" value="classpath:hibernate.cfg.xml"/&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan"&gt;
          &lt;list&gt;
              &lt;value&gt;emis.accounts.model&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;!-- Transaction --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>There's no configuration about <code>AOP</code> and <code>tx:advice</code>. So I tried to add some lines:</p>
  
  <pre><code>&lt;!-- Transaction --&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="*" propagation="REQUIRED" read-only="true"/&gt;
          &lt;tx:method name="get*" propagation="REQUIRED" read-only="true" /&gt;
          &lt;tx:method name="find*" propagation="REQUIRED" read-only="true" /&gt;
          &lt;tx:method name="list*" propagation="REQUIRED" read-only="true" /&gt;
          &lt;tx:method name="load*" propagation="REQUIRED" read-only="true" /&gt;
          &lt;tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="save" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="update" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="delete" propagation="REQUIRED" rollback-for="Exception" /&gt;
          &lt;tx:method name="*" propagation="REQUIRED" /&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  
  &lt;aop:aspectj-autoproxy /&gt;
  &lt;aop:config&gt;
      &lt;aop:pointcut id="aopPointcut"
          expression="execution(* emis.*.service.*.*(..))" /&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="aopPointcut"/&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <p>However, I got another exception:</p>
  
  <pre><code>WARNING: Exception thrown from ApplicationListener handling ContextClosedEvent
  java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: Root WebApplicationContext: startup date [Thu Mar 20 13:53:51 CST 2014]; root of context hierarchy
      at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:346)
      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333)
      at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:880)
  ...
  
  WARNING: Exception thrown from LifecycleProcessor on context close
  java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Thu Mar 20 13:53:51 CST 2014]; root of context hierarchy
      at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:359)
      at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:888)
      at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:841)
  ...
  </code></pre>
  
  <p>What should I do? Can anyone help me? Thanks a lot!!</p>