Switch DbEntity schema at runtime

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Switch DbEntity schema at runtime

Markus Reich
Hi,

I have a very special requirement :-)
We need to change the schema after logon (it's a JSF Tomcat WebApp)

I tried the following

After login

final String SESSION_CONTEXT_KEY =
SessionContextRequestHandler.class.getName() + ".SESSION_CONTEXT";
ObjectContext context = (ObjectContext)
session.getAttribute(SESSION_CONTEXT_KEY);
for (DbEntity dbEntity : context.getEntityResolver().getDbEntities()) {
if (dbEntity.getSchema().endsWith("crm")) {
if(Helper.getCrm()!=null) dbEntity.setSchema(Helper.getCrm());
} else if (dbEntity.getSchema().startsWith("simpleinv_")) {
if(Helper.getEupar()!=null) dbEntity.setSchema("simpleinv_" +
Helper.getEupar());
}
}
session.setAttribute(SESSION_CONTEXT_KEY, context);

The switching works, until a second session is opened! The first session
loses the schema and get the one from the second?

When I debug I have two different instances of the ObjectContext that's
perfect, but the DbEntities are the same :-/

regards
Meex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Switch DbEntity schema at runtime

John Huss
The DataMap (model) is shared by the contexts.  To make them different you
need to create two different ServerRuntimes.

On Fri, Nov 18, 2016 at 3:44 AM Markus Reich <[hidden email]>
wrote:

> Hi,
>
> I have a very special requirement :-)
> We need to change the schema after logon (it's a JSF Tomcat WebApp)
>
> I tried the following
>
> After login
>
> final String SESSION_CONTEXT_KEY =
> SessionContextRequestHandler.class.getName() + ".SESSION_CONTEXT";
> ObjectContext context = (ObjectContext)
> session.getAttribute(SESSION_CONTEXT_KEY);
> for (DbEntity dbEntity : context.getEntityResolver().getDbEntities()) {
> if (dbEntity.getSchema().endsWith("crm")) {
> if(Helper.getCrm()!=null) dbEntity.setSchema(Helper.getCrm());
> } else if (dbEntity.getSchema().startsWith("simpleinv_")) {
> if(Helper.getEupar()!=null) dbEntity.setSchema("simpleinv_" +
> Helper.getEupar());
> }
> }
> session.setAttribute(SESSION_CONTEXT_KEY, context);
>
> The switching works, until a second session is opened! The first session
> loses the schema and get the one from the second?
>
> When I debug I have two different instances of the ObjectContext that's
> perfect, but the DbEntities are the same :-/
>
> regards
> Meex
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Switch DbEntity schema at runtime

Andrus Adamchik
Yeah, that's pretty much the only way. Any other tricks will likely cause object id collisions in the cache, etc.

Andrus

> On Nov 18, 2016, at 6:55 PM, John Huss <[hidden email]> wrote:
>
> The DataMap (model) is shared by the contexts.  To make them different you
> need to create two different ServerRuntimes.
>
> On Fri, Nov 18, 2016 at 3:44 AM Markus Reich <[hidden email]>
> wrote:
>
>> Hi,
>>
>> I have a very special requirement :-)
>> We need to change the schema after logon (it's a JSF Tomcat WebApp)
>>
>> I tried the following
>>
>> After login
>>
>> final String SESSION_CONTEXT_KEY =
>> SessionContextRequestHandler.class.getName() + ".SESSION_CONTEXT";
>> ObjectContext context = (ObjectContext)
>> session.getAttribute(SESSION_CONTEXT_KEY);
>> for (DbEntity dbEntity : context.getEntityResolver().getDbEntities()) {
>> if (dbEntity.getSchema().endsWith("crm")) {
>> if(Helper.getCrm()!=null) dbEntity.setSchema(Helper.getCrm());
>> } else if (dbEntity.getSchema().startsWith("simpleinv_")) {
>> if(Helper.getEupar()!=null) dbEntity.setSchema("simpleinv_" +
>> Helper.getEupar());
>> }
>> }
>> session.setAttribute(SESSION_CONTEXT_KEY, context);
>>
>> The switching works, until a second session is opened! The first session
>> loses the schema and get the one from the second?
>>
>> When I debug I have two different instances of the ObjectContext that's
>> perfect, but the DbEntities are the same :-/
>>
>> regards
>> Meex
>>

Loading...