we have two different environments (test and production), each with it's own database connection.
The database connection is configured in the hibernate.cfg.xml, together with the mappings etc.
The hibernate.cfg.xml is part of the application - hence we cannot configure the database connection depending on the environment.
So we need some kind of configuration outside the application.
What is the best way for handling server specific database configurations with hibernate?
U can configure using annotations. below is exa
sessionFactory = new AnnotationConfiguration()
.addPackage("test.animals") //the fully qualified package name
.addAnnotatedClass(Flight.class)
.addAnnotatedClass(Sky.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Dog.class)
.addResource("test/animals/orm.xml")
.configure()
.buildSessionFactory();
Related
Does anyone know of software that can redirect a TDS client request to an appropriate instance of Sql Server based on the database catalog that the client is attempting to connect to?
In other words, say I have 3 database catalogs (dbcat1, dbcat2, dbcat3) and they're all hosted on the same server (sqlServer). The client code would have connection strings like "server=sqlServer;database=dbcat1" and "server=sqlServer;database=dbcat2", for example.
I would like some sort of (reverse) proxy software that would allow me to move one or more database catalogs to different server instances, but allow the client configuration to remain unchanged. For example, I could put dbcat1 and dbcat3 on sqlA, and put dbcat2 on sqlB, and sqlServer would be the new proxy software. It would pass the connection through to the appropriate SqlServer instance based on its configuration, and the client would have no idea it wasn't talking directly to a SqlServer instance.
Does such a thing exist, or do I get the "joy" of having to write something if I really, really want it? And, yes, I know this is not HA (High Availability) or DR (Disaster Recovery).
SqlServer Failover Clustering does not do this; it's for HA. It redirects at the server instance level (guest clustering)
SqlServer Availability Groups does not do this; it's for DR. You still need to know the AG that hosts the database you want (if you move the dbcat to another AG, you have to update your client config).
Microsoft Proxy Server was replaced by ISA and likely didn't do it anyhow.
Microsoft ISA (Forefront) is just a "dumb" proxy as far as I could tell.
I have a spring boot JDBC Cofiguration A as below which works well,
Configuration A
spring.datasource.sqlserver.url=jdbc:sqlserver://localhost:1433;database=my-testdb
spring.datasource.sqlserver.user=user_101#server-product1
spring.datasource.sqlserver.password=password_101
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
Configuration B below does not. Note that the url string does not have the database name in it. The database name is specified in a separate line, as one would expect to configure.
spring.datasource.sqlserver.url=jdbc:sqlserver://localhost:1433
spring.datasource.sqlserver.user=user_101#server-product1
spring.datasource.sqlserver.password=password_101
spring.datasource.sqlserver.database=my-testdb
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
Cannot figure out why the database name cannot be specified in a separate line and has to be in the URL. Can anyone mention why this does not work?
Other configuration : Spring Boot, sqljdbc41.jar
Well that's how the Microsoft's SQL Server / Azure JDBC driver works, database name is a mandatory part of the URL, and not given in a "database" attribute.
Technically, it may be related to the fact that databases in SQL Server behave rather autonomously, they each have their own set of catalog views, you cannot switch between databases freely etc.
According to Scaleout with SQL Server you can use SignalR.SqlServer to keep SignalR synced on a load balancing setup. I have an existing MVC 5 website with its own database created using Entity Code First. The article seems to use a dedicated database with service broker enabled and says not to modify the database.
So do I need to have a separate database for this? I know Entity can be picky if the database schema doesn't match and I worry that if I try to use the SignalR Sql Server package with the existing database that the tables it creates will cause a context changed error.
Also can someone provide me with more information about using the Microsoft.AspNet.SignalR.SqlServer package. The article I linked doesn't give a ton of detail and I don't know if I need to change anything in my hub and groups or if it is all handled automatically.
You should be able to, though you'd likely want to separate your entity framework definitions from signalR. You can either put SignalR in a separate database, or give the two a separate schema.
In terms of configuration, you'll need to make an addition to the Startup class of your web project:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var sqlConnectionString = "connection string here";
GlobalHost.DependencyResolver.UseSqlServer(sqlConnectionString);
this.ConfigureAuth(app);
app.MapSignalR();
}
}
I'm building an app around the servicestack framework and need to be able to access data in both Oracle and MS Sql Server. Is this possible using ORMLite, it seems that I can only set a single dialect for the App or have I missed something?
Yes it is possible and support for this is already built into the OrmLiteConnectionFactory, see the Master SQLServer + Sqlite shard example on OrmLite's project home page.
Basically you would register your default (or master) connection first with:
var dbFactory = new OrmLiteConnectionFactory(
"Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI",
SqlServerDialect.Provider);
Then you would register a named connection for every other connection you wish to support, e.g:
dbFactory.RegisterConnection("shard-1",
"~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(),
SqliteDialect.Provider);
Once that's configured, opening a connection without specifying a name will open a connection to the default database, e.g:
using (IDbConnection db = dbFactory.OpenDbConnection()) { ... } //Default DB
Whilst you can specify a name to open up a named connection to a db with a different provider, e.g:
using (var dbShard = dbFactory.OpenDbConnection("shard-1")) { ... } //Named DB
Manually use different Dialect Providers
The differences between the SQL Provider implementations between different RDBMS's are contained within each dialect provider. So if you want to use OrmLite's convenience extension methods against an specific ADO.NET provider implementation you just need to assign the ThreadStatic DialectProvider you wish to use, e.g:
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
var dbConn = new SqlConnection(SqlServerConnString);
dbConn.Select<Table>(); //All db access now uses the above dialect provider
This is essentially all what RegisterConnection in OrmLiteConnectionFactory automatically does behind the scenes for you.
For reference here are all the dialect providers for OrmLite up to this point:
SqlServerDialect.Provider
SqliteDialect.Provider (different 32/64 and Mono impls available)
MySqlDialect.Provider
PostgreSqlDialect.Provider
OracleDialect.Provider
FirebirdDialect.Provider
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.