Execute sql statement before normal execution with aop - sql

I'm trying to create a Multitenant application with spring.
I'm trying to have a different schema for each Tenant on a PostgreSql database.
I first created a TenantAwareDataSource extending org.springframework.jdbc.datasource.AbstractDataSource where basically I manage a Map of org.apache.commons.dbcp.BasicDataSource, configuring setConnectionInitSqls() for each tenant. (The datasource the project had before was org.apache.commons.dbcp.BasicDataSource)
But then discussing it with a friend, we came up with the idea of changing the schema for every statment executed with an aspect (aop), simply adding a set search_path to statement just before normal execution.
This could greatly simplify the problems related to having too many connections to the database (a connection pool for every tenant at any given time).
Has anybody executed additional statements using AOP?
Any pitfalls to overcome?
I'm thinking on put back org.apache.commons.dbcp.BasicDataSource and intercept java.sql.Statements.exe*(..)
I'm not very experienced with Spring persistence. Or SQL statement execution interception for that matter (haha).
Is it ok?
I found this article but I don't think I need to obtain a reference for each connection.
Am I right?
Also found this one. The author is using org.springframework.jdbc.core.JdbcOperations. Not sure it's the case in my Spring Roo generated project.
Thank you all.

Related

Ho do I call a web service on a database change?

There is a business process that happens currently in a totally manual fashion, which seems to me to be a no-brainer to automate. Basically what happens is when a new row is added to a single table in a SQL server, a person then manually makes a change in another business system (based on some very simple business logic).
It's easy to capture the business logic and make the change in the 2nd system programmatically using a web-service. However I don't know how to trigger the calling of the web service when the new row is added into the SQL table.
I've been reading from older forums posts that there are 2 options - the SQL dependency class in .NET and a database trigger in SQL. I was wondering if those are still the 2 main options for a problem like this one.
A colleague of mine showed me the SignalR framework and it was very impressive, but I understand that only works for web applications with a HTML/Javascript front end? I was wondering if there was something similar to that, that's easy to use to simply call a webservice.
Thanks
You can use sql dependency and you'll be notified each time your database changes
I wrote a tutorial a few years back on how to deal with this
Sql Dependency tutorial

Where to place registration logic for multi-tenanted database design?

I am currently in the process of turning a single-tenanted application into a multi-tenanted application.
As far as the database design goes, I have chosen the 'Shared Database, Separate Schema' solution as described within this article.
Following the instruction provided; I am required to perform the following steps, each and every time a new tenant is introduced:
Create new Schema for tenant.
Create new database user with access to the newly created schema(and only this schema).
Create necessary tables within the newly created schema.
My question is, where should this logic be placed?
My initial thought was within a stored procedure, however I am unsure of this decision and looking for some clarification.
I would like you to look into this implementation from a deployment perspective too.
Considering the fact that executing a job in Sql would be faster or even raising a tenant created event which can run a job that will be creating the necessary db infrastructure. At the end, the job will call a rest end point your app that can activate the tenant, so that the tenant can get into the app and use.
There are many options to be considered in the approach because it will be fully distributed and prone to failures.

How to synchronize between a custom KnowledgeSyncProvider and a SqlSyncProvider?

So, I need to synchronize two data stores, one of which is a SQL database, and I felt it was natural to use the built in provider for that side. But unfortunately, I started running into trouble because the SqlSyncProvider doesn't use the ChangeDataRetriever and NotifyingChangeApplier, but instead communicates through some DbSyncContext object. Therefore, I had to derive from the SqlSyncProvider and override mainly the GetChangeBatch and ProcessChangeBatch methods so they become compatible with the rest of the Sync Framework.
But the trouble is that I believe that I'm missing something in that transformation. The result is that when I create a row in a SQL database, and synchronize to the other store, and delete the row (or update) in the other store, after syncing the changes don't appear in the SQL database. The problem is probably caused by the bulkdelete stored procedure which filters the delete table and separates rows that are created locally from the rows created elsewhere.
Does anybody know what could cause this problem? I would really like to see some samples or documentation regarding synchronization between a SQL provider and a custom provider.

How to use SQL Cache Dependency Wtih EF DbContext?

I found this article on using Sql Cache Dependency with Linq2SQL.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=263
is it possible to do this same thing in Entity Framework with DbContext?
I have a multi-database app so each DbContext Instance Connection is different depending on the user.
But I feel like utilizing this technology would be the best way to invalidate the cache.
any help is appreciated.
Chase
Take a look at this, will give you a starting point: http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx, I tried it and it works fine but I have chosen to use a more custom and light weight approach.
Basically I retrieve the TraceString which is nothing but the raw SQL from my IQueriable<T> and create a standard SqlCacheDependency cache entry.
Your DbContext should live only for single unit of work. In your case for single request processing so there is no reason to involve any database dependency in EF.

Handling Multiple databases with NHibernate in a single application

At the moment I define the connection properties in a configuration file and only ever connect to one database. I'd like to be able to at some point have a user login, figure out (via a seperate central database maybe) what database they should be connected and from that point on all sessions created will talk to that database.
Whats the best way to achieve this. Create a configuration file for every possible database? Or could I have a single session manager and change the connection url on the fly accordingly? What sort of options do i have?
Update: Apologies I should have mentioned this was NHibernate. I didn't think it would matter but some things like Hibernate Shards will not be applicable to be as I believe NHibernate Shards is waiting.
You just need to make two datasources then call the one you need for the specific query.
Please take a look at this:
https://www.hibernate.org/450.html
Some official solutions.
And here:
http://www.java-forums.org/database/159-hibernate-multiple-database.html
an online thread about this issue.