title: How to simulate database failure to test 2-phase commit in Java
body: <p>I am implementing a 2-phase commit involving distributed resources. How do I simulate the failure of a participating database ? Pulling out the network cable doesn't work as it causes table deadlock. I am currently using hooks in my application code which throw <code>StaleConnectionException</code> at different points like before query execution, after query execution. My concern with this approach is:</p>
  
  <ul>
  <li>Is there a better way to simulate the DB failure?</li>
  <li>What happens to the connection object when DB connection goes bad? Does it retain its value or does it become null?</li>
  <li>What actually happens when application tries to reconnect to DB?What value does connection object get?Does it use an existing value from the connection pool? </li>
  </ul>
  
  <p>I would also like to test at intermediate points like during query execution, during commit (after <em>prepare</em> is sent, etc). Right now I put application into debug mode and step into the function call and pull the plug in between. But this approach is manual and won't work for a scale testing.</p>
  
  <p>Is there a simulator/emulator or tool which can help me do this?</p>