Multi-Tenancy EJB

Datetime:2016-08-23 01:57:41          Topic: EJB           Share

By ETWANG-Oracle onNov 25, 2015

Multi-Tenancy EJB

Benefit from the Multi-Tenancy support of WLS 12.2.1, EJB container gains a lot of enhancements. Application and resource "multiplication" allows for EJB container to provide MT features while remaining largely partition unaware. Separate application copies also brings more isolation, such as distinct Remote objects, beans pools, caches, module class loader instances etc. Below names a few of the new features you can leverage for EJB applications.

1. JNDI

  • Server naming nodes will be partition aware.
  • Applications deployed to partitions will have their EJB client views exposed in the corresponding partition's JNDI namespace. 

2. Security

  • Security implementation now allows multiple active realms, including support for per-partition security realm.
  • Role based access control, and credential mapping for applications deployed to partition will use the partition's configured realm.

3. Runtime Metrics and Monitoring

  • New ApplicationRuntimeMBean instance with the PartitionName attribute populated, will get created for every application deployed to a partition.
  • EJB container exposed Runtime MBean sub-tree will be rooted by the ApplicationRuntimeMBean instance.

4. EJB Timer service

  • Persistent local timers rely on the store component. Partitioned custom file stores provide the required isolation of tenant data.
  • Clustered timers under the hood use Job scheduler, which is also providing isolation.

5. JTA configuration at Partition Level

  • JTA timeout can be configured at partition level, in addition to domain level and EJB component level.
  • Timeout value in EJB component level takes precedence over the other two.
  • Support dynamic update via deployment plan.

6. WebLogic Store

  • Persistent local timers rely on the store component. Partitioned custom file stores provide the required isolation of tenant data. 

7. Throttling Thread Resource usage

  • Work Managers with constraints can be defined at global runtime level, and application instances in partitions can refer to these shared WMs to throttle thread usage across partitions esp. for non interactive use cases - batch, async, message driven bean invocations ...

8. Data Sources for Java Persistence API users

  • Persistence Units that use data sources defined as system resources in the Resource Group Template will be able to take advantage of the PartitionDataSourceInfoMBean based overrides.
  • Use cases requiring advanced customization can use the new deployment plan support being added for system resource re-configuration.
  • Persistence Units that use application packaged data source modules can use the current deployment plan support to have the copies in different partitions, point to the appropriate PDBs.

A sample EJB application leveraging Multi-Tenancy

Now we're going through a simple setup of an EJB application on MT environment to demonstrate the usage of some of these features.

The EJB application archive is named sampleEJB.jar, it includes a stateful session bean which interacts with database by JPA API. We want the application to be deployed to 2 separate partitions, each of which points to a database instance of its own, so they can work independently.

1.  Create Virtual Targets

The first step is to create 2 virtual targets for the 2 partitions respectively, which use different URI prefixes /partition1 and /partition2 respectively as showed below.

2.  Create Resource Group Template

Now we create a Resource Group Template named myRGT.  Resource Group Template is a new concept introduced by WLS 12.2.1, to which you can deploy your applications and different resources you need. This is very helpful when your application setup is complicated, because you don't want to repeat the same thing for multiple times on different partitions.

3.  Deploy application and data source

Now we can deploy the application and define the data source as below. Please pay attention that the application and the data source are all defined in myRGT scope.

4.  Create Partitions

Now everything is ready, it's time to create partitions. As the following image shows, we can apply the Resource Group Template just defined when creating partitions, it will deploy everything in the template automatically.

5.  Access the EJB

Now with the partitions created and started, you can lookup and access the EJB with the following code. We're using the URL for partition1 here, you can also change the URL to access another partition.

Hashtable<String, String> props = new Hashtable<String, String>();
  props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  props.put(Context.PROVIDER_URL, "t3://server1:7001/partition1");
  props.put(Context.SECURITY_PRINCIPAL, user);
  props.put(Context.SECURITY_CREDENTIALS, pass);
  Context ctx = new InitialContext(props);

  BeanIntf bean = (BeanIntf)ctx.lookup("MyEJB"); 
  boolean result = bean.doSomething(); 

6.  Override the data source

If you're feeling something is wrong, you're right. We defined the data source myDS in myRGT, then applied myRGT to both the partitions, now the 2 partitions are sharing the same data source. Normally we don't want this to happen, we need the 2 partitions to work independently without disturbing each other.  How can we do that?

If you want to make partition2 switch to another data source,  you can do that in the Resource Overrides tab of partition2 settings page. You can change the database URL here so another database instance will be used by partition2.

7.  Change the transaction timeout

As mentioned above, for EJB applications it's supported to dynamically change the transaction timeout value for a particular partition. This can also be accomplished in partition settings page. In the following example, we set the timeout to 15 seconds. This will take affect immediately without asking to reboot.

There're also some other things you can do in the partition settings page, such as defining a work manager or monitoring the resource usage for a particular partition. Spend some time you will find more very useful tools around here.





About List