Preparing Seed Data for Your Application on Google App Engine

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

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) {
			case SERVER_NAME: {
				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.


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

if (!dataExists) {
			if (runWithTestData){

and the setApplicationBootstrapData() method looks like this,

private void setApplicationBootstrapData() {


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

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.

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!

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. 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). To know more, send a mail to or visit
This entry was posted in Architecture, Cloud, Java and tagged , , , , , . Bookmark the permalink.

2 Responses to Preparing Seed Data for Your Application on Google App Engine

  1. Yogesh says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s