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

  • Spring Data JPA w/ EclipseLink : transaction issue

    Hello,

    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 :

    Code:
      import org.eclipse.persistence.sessions.server.ServerSession;
      import org.eclipse.persistence.tools.schemaframework.SchemaManager;
    
      @Service
      public class MyService {
        @PersistenceContext
        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 :

    Code:
    java.lang.IllegalStateException: No transactional EntityManager available
    	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
    	at $Proxy254.unwrap(Unknown Source)
    	at MyService.setup(MyService.java:10)
    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 :

    Code:
    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 [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1a52b3b] 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 [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1a52b3b]
    16:38:01,568 DEBUG JpaTransactionManager:548 - Closing JPA EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1a52b3b] 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.
Working...
X