title: Can't truncate PostgreSQL tables with JPA
body: <p>I'm trying to quickly delete all data from multiple tables using Spring Data JPA. From what I've read, the fastest way to do this is to truncate the tables.</p>
  
  <p>My code looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class DatabaseService {
  
      ... autowiring ...
  
      @Transactional
      public void deleteRepository(){
          repository.truncate();
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface repository extends JpaRepository&lt;Trip, Long&gt; {
  
      @Modifying
      @Query(value = "truncate table my_table",
              nativeQuery = true)
      void truncate();
  }
  </code></pre>
  
  <p>However, when I call the <code>deleteRepository()</code> method, I get the following exception:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databasePopulatorJob': Invocation of init method failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is org.hibernate.TransactionException: Unable to rollback against JDBC Connection
  ...
  Caused by: org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is org.hibernate.TransactionException: Unable to rollback against JDBC Connection
  ...
  Caused by: org.hibernate.TransactionException: Unable to rollback against JDBC Connection
  ...
  Caused by: java.sql.SQLException: Connection is closed
  ...
  </code></pre>
  
  <p>I'm using latest PostgreSQL database with JDBC driver version 42.2.5.</p>
  
  <p>I've also tried other methods to delete the data like <code>DELETE FROM my_table</code> (my_table contains about 2 million records), but it takes way too long. I'd appretiate any tips.</p>