Multiple Schema Configuration On Spring MVC + Hibernate + JPA -
i using 1 schema(called schemaadmin) in database transactions in app(s), every tables in 1 schema.
then company restructuring database management , require every app have 1 scheme read/write , allows readonly/select main schema, current 1 i'm using(schemaadmin).
so here data.xml file,
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="org.portal.data.repository" /> <tx:annotation-driven /> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory"/> </bean> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasource" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="database" value="${database.database}"/> <property name="databaseplatform" value="${database.databaseplatform}"/> <property name="showsql" value="${database.showsql}"/> <property name="generateddl" value="${database.generateddl}"/> </bean> </property> <property name="packagestoscan" value="org.portal.entity"/> </bean> <bean class="org.apache.commons.dbcp.basicdatasource" destroy-method="close" id="datasource"> <property name="driverclassname" value="${database.driverclassname}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> <property name="testonborrow" value="true" /> <property name="testonreturn" value="true" /> <property name="testwhileidle" value="true" /> <property name="timebetweenevictionrunsmillis" value="1800000" /> <property name="numtestsperevictionrun" value="3" /> <property name="minevictableidletimemillis" value="1800000" /> <property name="initialsize" value="1" /> <property name="maxactive" value="50" /> <property name="maxidle" value="20" /> </bean>
then config.properties file,
# database general setting database.database=oracle database.databaseplatform=org.hibernate.dialect.oracle10gdialect database.showsql=false database.generateddl=false database.driverclassname=oracle.jdbc.driver.oracledriver # development local http://localhost:8080 database.url=jdbc:oracle:thin:@192.168.1.1/orcl database.username=schemaadmin database.password=password security.login.callbackurl=http://localhost:8080/security/callback
the new schema in same database/url different username , password. confused on where/how connect 1 in file. read through info through google practically confused right now.
please advice. thank you.
the new schema in same database/url different username , password. i
with oracle, schema , user tightly related.
i propose no jta solution because configuration doesn't rely on java ee datasource.
address change should consider having 2 sets of datasources/entitymanagerfactory/transactionmanager : 1 read-only user/schema , read-write user/schema. of course, common configuration factored in properties.
to give idea, modified xml conf , started job (i didn't factor properties duplication should) :
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="org.portal.data.repository" /> <!-- original declaration no longer valid --!> <tx:annotation-driven transaction-manager="transactionmanagerread" /> <bean id="transactionmanagerread" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactoryread"/> </bean> <bean id="transactionmanagerreadwrite" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactoryreadwrite"/> </bean> <bean id="entitymanagerfactoryread" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasourceread" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="database" value="${database.database}"/> <property name="databaseplatform" value="${database.databaseplatform}"/> <property name="showsql" value="${database.showsql}"/> <property name="generateddl" value="${database.generateddl}"/> </bean> </property> <property name="packagestoscan" value="org.portal.entity"/> </bean> <bean id="entitymanagerfactoryreadwrite" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasourcereadwrite" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="database" value="${database.database}"/> <property name="databaseplatform" value="${database.databaseplatform}"/> <property name="showsql" value="${database.showsql}"/> <property name="generateddl" value="${database.generateddl}"/> </bean> </property> <property name="packagestoscan" value="org.portal.entity"/> </bean>
and for...
there tricky matter in spring configuration :
<tx:annotation-driven/>
it uses default transaction manager bean named transactionmanager
. should configure 1 of transaction managers did :
<tx:annotation-driven transaction-manager="transactionmanagerread" />
then, apply suitable transaction manager in processing, should specify in spring services transactionmanager
use (read or read-write). can define primary transactionmanager
used default. security, should transactionmanager
of limitation.
for example, in spring service bean reading, declare class use read-only transaction manager :
@transactional(transactionmanager="transactionmanagerread") public class customersearchservice(){ ...
of course, can define transaction manager @ method level.
ideally, if have many methods in writing , in reading, should check if can redesign or rearrange classes gather read-only , write methods. more maintainable , less error-prone.
Comments
Post a Comment