HSQLDB how to have multiple in memory databases - hsqldb

For instance, for each unit test I'd like to have a "different" database to use, but within the same JVM.

Appears that the "first time" you create an HSQL in-memory database, that becomes the "canonical" password:
String DB_CONNECTION_STR = "jdbc:hsqldb:mem:MySpecialTestDb";
String DB_USERNAME_STR = "sa";
String DB_USERNAME_PASSWORD = "";
DriverManager.getConnection(DB_CONNECTION_STR, DB_USERNAME_STR, DB_USERNAME_PASSWORD);
after this point, if you create a new connection to jdbc:hsqldb:mem:MySpecialTestDb it will connect to the same DB (and you'll need that same username and password unless you've run some privilege granting within it). So to create a second DB, just specify a different name, and/or password:
String DB_CONNECTION_STR = "jdbc:hsqldb:mem:AnotherTestDb";
String DB_USERNAME_STR = "sa"; // could use different here, doesn't matter
String DB_USERNAME_PASSWORD = "";
DriverManager.getConnection(DB_CONNECTION_STR, DB_USERNAME_STR, DB_USERNAME_PASSWORD);
and it will effectively create a new in memory database for you.
See also https://stackoverflow.com/a/23544323/32453

Related

PDO - connection - how to protect

$_pdo = new Data('mysql:host='.$db_host.';dbname='.$db_name.';port='.$db_port, $db_user, $db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$charset));
Variables come from form.
User can make a sql injection if I don't strip this variables?
Connect credentials should not come from user under any circumstances.
You have to store them in your server in some config file. And then use with connect string.
Please note that charset should be set in DSN, not options.
include 'settings.php';
$dsn = "mysql:host=$db_host;dbname=$db_name;port=$db_port;charset=$charset";
$pdo = new Data($dsn, $db_user, $db_pass);
If you are accepting input from a form to create the connection I would probably use some sanitization functions to clean it up before using it. If this is being stored in a text file or a database it would be a good idea to sanitize before it is saved as well before it is used.
http://php.net/manual/en/function.filter-var.php
$db_host = filter_var($db_host,FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding
quotes can be disabled by setting FILTER_FLAG_NO_ENCODE_QUOTES. Like
htmlspecialchars(), this filter is aware of the default_charset and if
a sequence of bytes is detected that makes up an invalid character in
the current character set then the entire string is rejected resulting
in a 0-length string.
http://www.php.net/manual/en/filter.filters.sanitize.php

Creating new database in code, RavenDB

I'm running RavenDB.Client 2.0.2173-Unstable. I'm creating a multi-tenant system, and as part of my registration process, i would like to create a new Raven database.
I have three simple lines of code..
string newDBName = "1234";
IDocumentStore documentStore = new DocumentStore { Url = "http://myserver:8080", DefaultDatabase = newDbName};
documentStore.Initialize();
documentStore.DatabaseCommands.EnsureDatabaseExists(newDBName);
Per suggestion, I also tried this:
string newDBName = "1234";
IDocumentStore documentStore = new DocumentStore { Url = "http://myserver:8080"};
documentStore.Initialize();
documentStore.DatabaseCommands.EnsureDatabaseExists(newDBName);
I get an InvalidOperationException on the last line, Raven is telling me it could not figure out what to do.
<h1>Could not figure out what to do</h1>
<p>Your request didn't match anything that Raven knows to do, sorry...</p>
I know my connection/server works, because I'm able to read/write from the default database.
Is it a permissions issue?
You can't create a database from the database you're already in. Leave the DefaultDatabase field blank so you connect to the RavenDB system database. You should then be able to create the new tenant database.
You should also make sure to pass the tenant database name when opening sessions, creating indexes, and using DatabaseCommands.
In Ravendb v4, you can use the CreateDatabaseOperation to create a new database on the server.
store.Maintenance.Server.Send(
new CreateDatabaseOperation(new DatabaseRecord("MyNewDatabase")));

F# Type Provider for SQL in a class

I am writing a F# to be used with Azure Worker role. I want the class to have the connection string a as a parameter. I create a db connection with
type dbSchema = SqlDataConnection<"...">
let db = dbSchema.GetDataContext()
but dbSchema is a type so it cannot be embeded in my class (another type). I can create two separate modules, one with the db connection and another one with my class
module DataSource =
[<Literal>]
let connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=Service;Integrated Security=True"
type dbSchema = SqlDataConnection<connectionString>
let db = dbSchema.GetDataContext()
module DealerFactory =
type Factory(connectionString) =
member this.GetList(latitudeLeftTop, longitudeLeftTop, latitudeRightBottom, longitudeRightBottom) =
".."
But how do I use the connectionString in my class' constructor to create the connection?
The type provider for SQL database uses connection string for two different purposes. First, it needs one (at compile time) to generate the database schema. Second, you may (optionally) give it another one to use at runtime when actually running the program.
The compile-time connection string needs to be specified as parameter in SqlDataConnection<...> and the run-time connection string can be passed to GetDataContext(...) operation.
So, you can define your type using statically known compile-time connection string:
[<Literal>]
let connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=Service; ..."
type dbSchema = SqlDataConnection<connectionString>
And when you want to create an instance of the DB connection, you can pass it another connection string:
type Factory(connectionString) =
// Create connection to the DB using (a different)
// connection string specified at runtime
let db = dbSchema.GetDataContext(connectionString)
member this.GetList( latitudeLeftTop, longitudeLeftTop,
latitudeRightBottom, longitudeRightBottom) =
// Use local 'db' to access the database
query { for v db.Table do select v }
Compared with your original code (with the db value in a module), there is a difference that this creates a new db instance for every Factory, but I guess this is expected if Factory takes the connection string as an argument.

How do I use application settings to store MySQL database connection info?

I'm creating an in-house application and have always hardcoded the database connection string. However, this time I want to do something different and give the users the ability to enter the information from the application.
I figured out that I can store the variables in the Application Settings and call them from code, but I can't figure out how to call them within the connection string.
Here's the code:
Dim dbConn As New MySqlConnection
dbConn.ConnectionString = "Server=172.43.96.271;Port=3306;Uid=someone;
Password=theirpassword;Database=thedb"
Hope I explained myself well?
You can simply concatenate the string together, or better yet, use the String.Format method:
dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database={4}", My.Settings.Server, My.Settings.Port, My.Settings.Uid, My.Settings.Database)
If you were using MS SQL, I'd recommend using the SqlConnectionStringBuilder class to do it, but since you're using MySql, it doesn't really apply. You may be able to use it anyway, though.
You would have to use User Settings for this.
And, if you want the users to input the separate parts of the connection string (Server, Post, Username, Password and DB), you would have to create a settings entry for each of those, and then construct the connection string from those values.
Here's a good article for this: User Settings Applied

Applying SSIS Package Configuration to multiple packages

I have about 85 SSIS packages that are using the same connection manager.
I understand that each package has its own connection manager.
I am trying to decide what would be the best configurations approach to simply set the connectionstring of the connection manager based on the server the packages are residing on.
I have visited all kinds of suggestions online, but cannot find anywhere the practice where I can simply copy the configuration from one package to the rest of the packages.
There are obviously many approaches such as XML file, SQL Server, Environment Variable, etc.
All the articles out there are pointing to use an Indirect method by using XML or SQL approach. Why would using an environment variable for just holding a connection string is such a bad approach?
Any suggestions are highly appreciated.
Thanks!
Why would using an environment variable for just holding a connection string is such a bad approach?
I find the environment variable or registry key configuration approach to be severely limited by the fact that it can only configure one item at a time. For a connection string, you'd need to define an environment variable for each catalog on a given server. Maybe it's only 2 or 3 and that's manageable. We had a good 30+ per database instance and we had multi-instanced machines so you can see how quickly this problem explodes into a maintenance nightmare. Contrast that with a table or xml based approach which can hold multiple configuration items for a given configuration key.
...best configurations approach to simply set the connectionstring of the connection manager based on the server the packages are residing on.
If you go this route, I'd propose creating a variable, ConnectionString and using it to configure the property. It's an extra step but again I find it's easier to debug a complex expression on a variable versus a complex expression on a property. With a variable, you can always pop a breakpoint on the package and look at the locals window to see the current value.
After creating a variable named ConnectionString, I right click on it, select Properties and set EvaluateAsExpression equal to True and the Expression property to something like "Data Source="+ #[System::MachineName] +"\\DEV2012;Initial Catalog=FOO;Provider=SQLNCLI11.1;Integrated Security=SSPI;"
When that is evaluated, it'd fill in the current machine's name (DEVSQLA) and I'd have a valid OLE DB connection string that connects to a named instance DEV2012.
Data Source=DEVSQLA\DEV2012;Initial Catalog=FOO;Provider=SQLNCLI11.1;Integrated Security=SSPI;
If you have more complex configuration needs than just the one variable, then I could see you using this to configure a connection manager to a sql table that holds the full repository of all the configuration keys and values.
...cannot find anywhere the practice where I can simply copy the configuration from one package to the rest of the packages
I'd go about modifying all 80something packages through a programmatic route. We received a passel of packages from a third party and they had not followed our procedures for configuration and logging. The code wasn't terribly hard and if you describe exactly the types of changes you'd make to solve your need, I'd be happy to toss some code onto this answer. It could be as simple as the following. After calling the function, it will modify a package by adding a sql server configuration on the SSISDB ole connection manager to a table called dbo.sysdtsconfig for a filter named Default.2008.Sales.
string currentPackage = #"C:\Src\Package1.dtsx"
public static void CleanUpPackages(string currentPackage)
{
p = new Package();
p.app.LoadPackage(currentPackage, null);
Configuration c = null;
// Apply configuration Default.2008.Sales
// ConfigurationString => "SSISDB";"[dbo].[sysdtsconfig]";"Default.2008.Sales"
// Name => MyConfiguration
c = p.Configurations.Add();
c.Name = "SalesConfiguration";
c.ConfigurationType = DTSConfigurationType.SqlServer;
c.ConfigurationString = #"""SSISDB"";""[dbo].[sysdtsconfig]"";""Default.2008.Sales""";
app.SaveToXml(sourcePackage, p, null);
}
Adding a variable in to the packages would not take much more code. Inside the cleanup proc, add code like this to add a new variable into your package that has an expression like the above.
string variableName = string.Empty;
bool readOnly = false;
string nameSpace = "User";
string variableValue = string.Empty;
string literalExpression = string.Empty;
variableName = "ConnectionString";
literalExpression = #"""Data Source=""+ #[System::MachineName] +""\\DEV2012;Initial Catalog=FOO;Provider=SQLNCLI11.1;Integrated Security=SSPI;""";
p.Variables.Add(variableName, readOnly, nameSpace, variableValue);
p.Variables[variableName].EvaluateAsExpression = true;
p.Variables[variableName].Expression = literalExpression;
Let me know if I missed anything or you'd like clarification on any points.