Announcement Announcement Module
Collapse
No announcement yet.
Spring-Data - Set/Define TransactionManager to Repository Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring-Data - Set/Define TransactionManager to Repository

    Hi,

    how can i inject a TransactionManager to a Repository created in a "Standalone Way"

    RepositoryFactorySupport factory = … // Instantiate factory here
    UserRepository repository = factory.getRepository(UserRepository.class);

    The Problem.. i need each Repository twice..

    On the one hand to have transactional access to the db-master , and on the other hand to have "read only" access " to the db-slave


    Using the <jpa:repositories.. there is the possibility to inject a special TransactionManager, but i can not use this in that way.
    Last edited by thieste; Mar 9th, 2012, 02:40 AM.

  • #2
    Little confuse your case

    But you should has some service business classes annotated with @Service and @Transactional that call or use some DAO or Repositories annotated with @Repository and @Transactional too

    how can i inject a TransactionManager to a Repository created in a "Standalone Way"

    RepositoryFactorySupport factory = // Instantiate factory here
    UserRepository repository = factory.getRepository(UserRepository.class);
    Why do you want to do that? Could you explain your case?

    Comment


    • #3
      Hi dr_pompeii,

      you are right, without some background information it is not clear what i i want to do, as you said it is confuse.

      Well, i want to create a central service layer for easy access to relational databases. In my company i am working for, we have a lot of applications, accessing different or sometimes the same mysql-databases. Until now, every developer must deal with hibernate and will faces with all the problems.
      The base idea of this service layer is, to provide an general library for accessing relational databases by using JPA. The developer of an app only need to add this lib and set some db specifics settings, like url, username, password, hibernate.properties e.g.) in a properties file for accessing a special db.

      see attached file (Klassendiagramm.jpg)

      For JPA based access i want to use Spring-Data-JPA.


      So, I hope is much clearer what i want to do ...


      The Problem is as followed:


      For each database(master) exists a slave. To unload (i hope this is the right word) the master, some read only operation should be done by reading from the slave


      Well and this one of my problems, dealing this service layer.


      To avoid redundant code and making creation of a new servicelayer for further databases much easier, i use a custom BeanFactoryPostProcessor, creating and adding BeanDefinitions for DataSource, EntityManagerFactory, EntityManager and TransactionManager. Each concrete db service layer applicationContext must add this bean with the properties:

      bean-package (package path of the entities)
      repository-base-package
      service-base-package


      Thats it, all necessary bean definition will be created and added to the context of each concrete db service layer..


      for master the bean name has no suffix, for slave each beandefintion has the suffix (suprise, suprise) Slave

      so the application contains beandefinition like:

      shopDB.entityManagerFactory
      shopDB.entityManager
      shopDB.shopBaseService

      shopDB.entityManagerFactorySlave
      shopDB.entityManagerSlave
      shopDB.shopBaseServiceSlave


      Using the repository-base package i want to create in the same way an repository for master (shopDB.shopRepository) and slave (shopDB.shopRepositorySlave) which will be injected in the correct shopBaseService beanDefinition

      To create the Repository i use the "Standalone way" descriped in the spring-data-reference docu by using the JpaRepositoryFactory.. but the Problem is, that i don't know where to set/define the correct transactionManager.

      shopDB.shopRepository should use the shopDB.transactionManager


      My current workaround is to define the repositories (master)
      using <jpa:repostories /> setting the entityManagerRef and transactionManagerRef... this work perfectly , but it would be great if i can define this in the custom BeanFactoryPostProcess, too.


      Another problem with the usage of the <jpa:repositories /> namespacehandler is, that the beanname the repository is only the SimpleName not the CanonicalName etc.. if i have two different DBs, with a shopRepository and both db service layers are used in one application ... the repository bean name is not unique ....
      Last edited by thieste; Mar 11th, 2012, 08:09 AM.

      Comment

      Working...
      X