Preparing Seed Data for Your Application on Google App Engine

Table of contents
Reading Time: 3 minutes

For the past few weeks, we have been getting requests on our website about the way we generated seed data for our multi-tenant application. I thought it would be best to talk about the method that we used as a part of a separate post.

If you have been following our blog, you would notice that TE-CON launched the public beta of This is a an enterprise timesheet and invoicing application which is also multi-tenant. Thanks to the namespace api, we could convert the application to multi-tenant architecture in a matter of four hours. So how do we seed data for each tenant?

As soon as we get a request from a tenant like, the filter knows that the tenant is “amazon”. The namespace parameter is set for each request. Let us look at the filter logic

[sourcecode language=”java”]
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {

// If the NamespaceManager state is already set up from the
// context of the task creator the current namespace will not
// be null. It’s important to check that the current namespace
// has not been set before setting it for this request.
if (NamespaceManager.get() == null) {
switch (strategy) {
String namespaceKey = request.getServerName();
NamespaceManager.set(namespaceKey);"The namespace for the request is: " + namespaceKey);
checkSeedDataAndNamespace(request, namespaceKey);

case EMPTY: {

chain.doFilter(request, response);

Once we get the tenant information from the request, we call a method called checkSeedDataAndNamespace(request, namespaceKey);

This method checks if the seed data is already present for this namespace. If it is not, then it is created else nothing is done.

Of course for added optimization, we check the memcache if the seed data exists instead of going to the datastore.


[sourcecode language=”java”]
private void checkSeedDataAndNamespace(ServletRequest request, String namespaceKey) {
HttpSession httpSession = ((HttpServletRequest) request).getSession();
if (StringUtils.isEmpty((String) httpSession.getAttribute("isSeedDataSet"))) {
if (CacheController.get(namespaceKey) == null) {
CacheController.put(namespaceKey, "Seed Data Set");
httpSession.setAttribute("isSeedDataSet", "true");

Next, let us see how do we do the seeddata setup, just in case it does not exist

So, we have a method like

[sourcecode language=”java”]
if (!dataExists) {
if (runWithTestData){

and the setApplicationBootstrapData() method looks like this,

[sourcecode language=”java”]
private void setApplicationBootstrapData() {


As a sample, the setting up of user department happens like this

[sourcecode language=”java”]
private void setUserDepartment() {
UserDepartment department = new UserDepartment();

UserDepartment hrDepartment = new UserDepartment();
hrDepartment.setName("Human Resources");


Likewise we set up rest of the seed information which is required for a tenant. You would notice that since we are using JPA in our application, we call the persist on our GenericDAOJpaImpl class.

[sourcecode language=”java”]
public T persist(T domObj) {
return domObj;

Hence, with the namespace api and the namespace filter, it becomes very easy to check if the seed data for a tenant is already set or it needs to be set for the first time.

You would like to keep some things in mind. There is a 30 second deadline at the app engine. If you feel that your seeding is going to take more than that then it is better to break the seeding into a few parts. First, set the part where the admin of the application is allowed to get in and is allowed to get to the admin screens. Once the admin is in the process of setting up the infrastructure you could seed other parts of the application as and when required.

Happy seeding!

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to or visit

2 thoughts on “Preparing Seed Data for Your Application on Google App Engine3 min read

  1. nice article, one thing though more related to first code segment, what about making possible value of strategy as ENUM with an interface which have method setNamespace(). The switch case statements would move to enums.
    so at the end switch will be gone and you can say strategy.setNamespace() and depending upon type of enum it will set the namespace.

Comments are closed.