2 Replies Latest reply on Sep 9, 2011 10:43 AM by Angelo D'Amato

    multidatabase options for solution based on EntityHome

    Angelo D'Amato Newbie

      Hi to all,

      For CRUD operation i use EntityHome and EntityQuery.
      All applications work fine with one database, now i would have 2 identical database : a production database and an internal database. In some condition i want that when user doing some operation on an entity, this information will store not only in internal database but also in production database.
      I have seen that EntityHome manages just one default entityManager , my idea is to inject in EntityHome a second EntityManager for manage conditional CRUD operation in production database.
      I have searched possible options for multi database in seam but nothing related to EntityHome.
      there are other efficient solutions for manage multidatabase solution using EntityHome and EntityQuery ?
      thanks,



      Angelo D'Amato

        • 1. Re: multidatabase options for solution based on EntityHome
          gebuh Newbie

          What about creating another persistence context, assigning that to a different entityManager and then injecting that into your EntityHome?

          • 2. Re: multidatabase options for solution based on EntityHome
            Angelo D'Amato Newbie

            I have 2 data sources .. EXAMPLE1-ds.xml and EXAMPLE2-ds.xml .

            Databases schema are identical. maybe i must use xa-datasource for distributed db not no-tx-datasource.


            EXAMPLE1-ds.xml



            <no-tx-datasource>
                            <jndi-name>EXAMPLE1-DS</jndi-name>
                            <use-java-context>true</use-java-context>
                            <connection-url>jdbc:mysql://localhost:3306/example1</connection-url>
                            <driver-class>com.mysql.jdbc.Driver</driver-class>
                            <user-name>example</user-name>
                            <password>passwd</password>
                            <min-pool-size>5</min-pool-size>
                            <max-pool-size>20</max-pool-size>
                            <blocking-timeout-millis>5000</blocking-timeout-millis>
                            <idle-timeout-minutes>4</idle-timeout-minutes>
                            <metadata>
                                    <type-mapping>MySQL</type-mapping>
                            </metadata>
            </no-tx-datasource>






            EXAMPLE2-ds.xml



            <no-tx-datasource>
                            <jndi-name>EXAMPLE2-DS</jndi-name>
                            <use-java-context>true</use-java-context>
                            <connection-url>jdbc:mysql://localhost:3306/example2</connection-url>
                            <driver-class>com.mysql.jdbc.Driver</driver-class>
                            <user-name>example</user-name>
                            <password>passwd</password>
                            <min-pool-size>5</min-pool-size>
                            <max-pool-size>20</max-pool-size>
                            <blocking-timeout-millis>5000</blocking-timeout-millis>
                            <idle-timeout-minutes>4</idle-timeout-minutes>
                            <metadata>
                                    <type-mapping>MySQL</type-mapping>
                            </metadata>
            </no-tx-datasource>







            my PERSISTENCE.XML  -- i use seam managed transaction




            <?xml version="1.0" encoding="UTF-8"?>
            <!-- Persistence deployment descriptor for dev profile -->
            <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                    version="1.0">
                    <persistence-unit name="example1-pu" transaction-type="JTA">
                            <provider>org.hibernate.ejb.HibernatePersistence</provider>
                            <jta-data-source>java:EXAMPLE1-DS</jta-data-source>
                            <class>it.example.MyClass</class>
                            
                            <properties>
                                    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                                    <property name="hibernate.hbm2ddl.auto" value="update" />
                                    <property name="hibernate.show_sql" value="false" />
                                    <property name="hibernate.connection.release_mode" value="after_statement"></property>
                                    <!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
                                    <property name="hibernate.transaction.manager_lookup_class"
                                            value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                            </properties>
                    </persistence-unit>
            
            
                    <persistence-unit name="example2-pu" transaction-type="JTA">
                            <provider>org.hibernate.ejb.HibernatePersistence</provider>
                            <jta-data-source>java:EXAMPLE2-DS</jta-data-source>
                            <class>it.example.MyClass</class>
                            
                            <properties>
                                    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                                    <property name="hibernate.hbm2ddl.auto" value="update" />
                                    <property name="hibernate.show_sql" value="false" />
                                    <property name="hibernate.connection.release_mode" value="after_statement"></property>
                                    
                                    <property name="hibernate.transaction.manager_lookup_class"                    value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                            </properties>
                    </persistence-unit>
            
            </persistence>
            






            I have generated my application with seam-gen. All entities are managed from EntityHome and EntityQuery.



            mine strategy is this :




            I created a class named ProductionDBEntityHome that replicate information on a EXAMPLE2 DATABASE





            public class ProductionDBEntityHome<E> extends EntityHome<E> {

                   

                    @In(value="entityManagerProduction")
                    EntityManager entityManagerProduction;
                   
                    @Override
                    @Transactional
                    public String remove() {

                           
                            String remString = super.remove();
                           
                            entityManagerProduction.remove(entityManagerProduction.merge(getInstance()));
                            entityManagerProduction.flush();
                           
                            return remString;
                    }

                    @Override
                    @Transactional
                    public String persist() {
                   
                            String persistString = super.persist();
                           
                            entityManagerProduction.merge(getInstance());
                             entityManagerProduction.flush();

                            return persistString;
                    }

                    @Override
                    @Transactional
                    public String update() {
                           
                            String updateString = super.update();
                           
                           
                            entityManagerProduction.merge(getInstance());
                            entityManagerProduction.flush();
                           
                            return updateString;
                    }
                   
                   
                    public String updateUserPasswordHash(UserAccount user){
                            UserAccount dbinternal = entityManagerProduction.find(UserAccount.class, user.getId());
                           
                            UserAccount dbproduction = entityManagerProduction.find(UserAccount.class, user.getId());
                           
                            dbproduction.setPasswordHash("");
                           
                            entityManagerProduction.merge(dbproduction);
                            entityManagerProduction.flush();
                            return dbinternal.getPasswordHash();
                    }

            }






            and i extend all my controller classes with ProductionDBEntityHome




            an Example i have a bean named BankInformation there is a class that manage all CRUD operation named BankInformationHome that normally extend EntityHome.
            But I change extention with ProductionDBEntityHome



            BankInformationHome extends ProductionDBEntityHome




            and all data are replicated but i have a doubt that this is not the correct mode to do replication of data.