Results 1 to 3 of 3

Thread: Spring-Data - Set/Define TransactionManager to Repository

  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Question 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 at 01:40 AM.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Default

    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 ....
    Attached Images Attached Images
    Last edited by thieste; Mar 11th, 2012 at 08:09 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •