RESTful API: Connect to specific database based on URL/endpoint - database

Let's say there are two databases customer1 and customer2, and I want to use one codebase to access them i.e connect to a database based on the URL.
www.example.com/customer1/dashboard would connect to customer1.
www.example.com/customer2/dashboard would connect to customer2.
What is the proper way to do this?
Do I name the database the same as the URL parameter, and then pass that parameter to the connection bit, or is there a better way?

Related

NodeJS - modifying one object from two different Node servers

Let's say I have an array in a node server at IP xx.xx.xx, let's call it mobile server because only mobile users can access it:
var users = [{username: "jim", stats: "x"}]
Now I have another node server at IP yy.yy.yy, for PC users only.
I want user "jim" to be able to access his user through HTTP requests from his PC and his mobile aswell, but also modify his object whenever he makes changes in one of the devices. is it possible to achieve, performance-wise and security-wise?
Yes, this is possible. You would want both node servers to access the same db for simplicity. You can use any database, nosql (mongodb) or a traditional sql one (mysql, postgresql, etc).

How to connect sql server with swift

I'm working now on an application for iOS (using swift), the database is already exist in SQL Server.
How I will use it and connect with it? Do i need a web service to do that?
thanks all .
It is recommended to use a web service since having the application talk directly to the database means you need to include the SQL Credentials in the binary and anyone with a copy of the application can get them and do whatever they wish in the database. From a security point of view, this is bad.
The correct approach is to have a web server which will host an "API" -- a web application that will receive HTTP requests from the app and translate them to database queries and then will return the response in another format, such as JSON.
However, you need to be careful. This web services must use HTTPS and must first validate the input in order to protect against attacks such as SQL Injection.

how to display the database name and change the database properties in java (spring framework)

I have a requirement to display the database name on the screen that is connected by the web application (which is configured through the datasource on weblogic server with spring data xml having all the configurations) and also is there any way to switch from current database to different datasource (database server) while working/running on the web application (with user screen).
Thanks.
As far as I know, there is no straight-forward way to know this info. One way I can think of doing this is to parse the spring config XML file using an XML parser for the desired element - even that too will only give you the JNDI name of the datasource your app would be using. I hope you have a mechanism to determine which JDBC JNDI name maps to which database. If you don't have that information, you would have to use JMX (MBeans) to connect to the Weblogic environment to get that info.

Testing Connection Parameters with NHibernate

We have a program where users can specify their database connection parameters. The usual suspects including host, port, username, password, and table name. We are connecting to the database using NHibernate. What we'd like to do is be able to build the configuration with NHibernate and then test the connection parameters before continuing with other operations; notifying the user of failure.
Is this possible to do through NHibernate, or will it require using each database type we support's specific driver and creating a custom TestConnection() method for each type?
I realize this is an old post - but I guess an answer to a question never hurts.
I don't think there is a way to explicitly tell NHibernate to test out the connection string. However, when you instantiate the SessionFactory it will attempt to connect to the database. You could wrap your SessionFactory creation in a Try/Catch and handle the error that way.
I use Fluent NHibernate, but I'm sure the following example will still explain the situation.
Dim sf as SessionFactory
Try
sf = CreateSessionFactory()
Catch ex as FluentNHibernate.Cfg.FluentConfigurationException
Messagebox.Show(ex.InnerException.Message)
End Try
The ex.InnerException.Message contains the actual error and will tell you if:
The connection string was invalid
The server could not be found
The user/pass could not be authenticated
To configure Nhibernate you have two options:
Set the dialect when you are building the session factory. This will assign reasonable default value to Nhibernate's ADO and other configuration values.
Manually set the configuration values.
That said, at some point, you need to configure Nhibernate to use the appropriate driver for the database you want to talk to. Which means you need to be able to build Session Factories of different types (your supported database types). To do this you need more than just "host, port, username, password, and table name". You need to know the database type(Dialect).
If you intend to just try to connect the database with every driver available to you not knowing what the database type is, you may run into problems when the database and the dialect don't match. Imagine you use a SqlServer2008 dialect on SqlServer2005 machine. The difference in dialect can cause a particular SqlServer2008 feature you are using not to, obviously, work. Moreover, if you don't stick to basic SQL through out all your code, you may be generating Sql that works, say, in PostgreSql but not in SqlServer (Think sequences and such).
To learn more about configuring Nhibernate read:
Chapter 3: Session Factory Configuration. Specially sections 3.3, 3.4, 3.5 which talk about configuration parameters.
Last note, Nhibernate supports multiple databases. But, for complex domain layers where you rely on database specific constructs, your code doesn't.

Connect to the same database, but as a another user, without hardcoding a connection string

Can I use OPENDATASOURCE (or another mechanism) from a Stored Procedure to connect to the same database as a different user? If so, how?
The database is meant to be deployed to several customers, and replicated by them as many times as they want to, etc. For this reason, I CANNOT HARDCODE the database server's name or the database's name.
(I tried using OPENDATASOURCE, but it only accepts hardcoded connection strings.)
Might EXECUTE AS work in your situation? http://msdn.microsoft.com/en-us/library/ms181362.aspx
You can set up a Linked Server to connect to the remote server using the login's current security context (or other options as it applies to your situation).
From your stored procedure, you could access it with something like SELECT * FROM mylinkedservername.mylinkedserverdatabase.dbo.mytable
But you say you want to connect to the same database but using a different login? You're looking for impersonation. Perhaps you can do this making a Linked Server that references itself, I haven't tried it. Search Microsoft Help documentation for how to set it up normally and test if it does what you're looking to do.

Resources