Announcement Announcement Module
No announcement yet.
Spring Data JPA w/ EclipseLink : transaction issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data JPA w/ EclipseLink : transaction issue


    I'm using Spring Data JPA along with EclipseLink, targeting a PostgreSQL database.
    While developing my app, I'd like to have more flexibility than simply the static "persistence.xml" configuration file.
    In particular, there's a "eclipselink.ddl-generation" parameter than can be set to : "none", "create-tables" or "drop-and-create-tables".
    As you probably know, at application startup, EclipseLink sets up the database according to this parameter.
    Now, I'd like to be able to do the same programatically, at any time I need to.

    But when I try to get access to Eclipse's ServerSession :

      import org.eclipse.persistence.sessions.server.ServerSession;
      public class MyService {
        private EntityManager entityManager;
        public void setup() {
          ServerSession session = entityManager.unwrap(ServerSession.class);
          SchemaManager schemaManager = new SchemaManager(session);
          schemaManager.replaceDefaultTables(true, true);
    I get this error :

    java.lang.IllegalStateException: No transactional EntityManager available
    	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(
    	at $Proxy254.unwrap(Unknown Source)
    	at MyService.setup(
    The solution I found is to add a @Transactional annotation to the method. But I suppose there's no reason to run my code in a transaction.
    It runs fine... except that no change is made to the database!
    The log is as follows :

    16:38:01,567 DEBUG JpaTransactionManager:365 - Creating new transaction with name [MyService.setup]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    16:38:01,568 DEBUG JpaTransactionManager:323 - Opened new EntityManager [[email protected]] for JPA transaction
    16:38:01,568 DEBUG JpaTransactionManager:752 - Initiating transaction commit
    16:38:01,568 DEBUG JpaTransactionManager:462 - Committing JPA transaction on EntityManager [[email protected]]
    16:38:01,568 DEBUG JpaTransactionManager:548 - Closing JPA EntityManager [[email protected]] after transaction
    16:38:01,568 DEBUG EntityManagerFactoryUtils:338 - Closing JPA EntityManager
    I know my question is EclipseLink-specific but I have the feeling that Spring Data JPA is having a bad interaction here.

    Thanks for your help.