I am running a raw query on Laravel using DB::statement() method. This statement needs to fetch data from my secondary database.
I wonder if there is any clean way to specify database connection to this method instead of using database name prefix before all fields.
Yes, you can use the connection method, which accepts any connection name stored in the config/database.php file:
DB::connection('pgsql')->statement('your statement here...');
You can read more in the Using Multiple Database Connections Documentation.
Related
In GRDB I can create read-only database connections with
try dbPool.read { db in
I can create read-write database connections with
try dbPool.write { db in
In each case I can call a function from within the closure using db as an argument. In GRDB read-only database connections can run in parallel in different threads. Write database connections block the database.
I have a pattern I would like to use in some functions:
Select a record filtered on some attributes.
If a record is found, return it.
If no record is found, create a default placeholder record with those attributes.
If the db is read-write, save the default placeholder and return it.
If the db is read-only, just return the placeholder
Is there a way to determine if the db is read-only? Looking through the reference documentation at http://groue.github.io/GRDB.swift/docs/4.1/Classes/Database.html, I don't see a simple way to determine this aside from error handling.
I could just attempt to save in the function and catch the error if the db is read-only.
Is there a way to determine if the db is read-only?
No, there isn't any public way to get this information. It would be possible, but the feature request hasn't shown up yet.
I could just attempt to save in the function and catch the error if the db is read-only.
Correct.
I have a pattern I would like to use in some functions [...]
The pattern you describe looks like a premature optimization for a plain simple write. Do you notice actual performance problems?
I use Sequel::Model.DB to interact with my DB, but for some reason, the DB structure was changed, for example, via the DB console.
This method:
Sequel::Model.db.schema('table_name')
still returns the old DB, cached from the first connection I guess.
How can I reset that cache or, ideally, ensure the actual DB connection on each request?
I tried to use a new connection every time:
def db
#db ||= Sequel.connect(Sequel::Model.db.opts)
end
but, predictably, I got this error eventually:
Sequel::DatabaseConnectionError - PG::ConnectionBad: FATAL: sorry, too many clients already
You shouldn't be changing the structure of the database in an incompatible way while Sequel is running. The easiest way to solve this issue is just to restart the process after changing the database schema, and Sequel will pick up the new database structure.
If you really wanted to try to do this without restarting the process, you could remove the cached schemas (#db.instance_variable_get(:#schemas).clear), and reset the dataset for all model classes (ModelClass.dataset = ModelClass.dataset for each Sequel::Model). However, that doesn't necessarily result in the same thing, since if you remove a column, the old column name will still have a method defined for it.
I have an Apache Spark Job and one of its components fires queries at Apache Ignite Data Grid using Ignite SQL and the query is a SQLFieldsQuery. I was going through the thread dump and in one of the Executor logs I saw the following :
org.h2.mvstore.db.TransactionStore.begin(TransactionStore.java:229)
org.h2.engine.Session.getTransaction(Session.java:1580)
org.h2.engine.Session.getStatementSavepoint(Session.java:1588)
org.h2.engine.Session.setSavepoint(Session.java:793)
org.h2.command.Command.executeUpdate(Command.java:252)
org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:130)
org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:115)
org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.connectionForThread(IgniteH2Indexing.java:428)
org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.connectionForSpace(IgniteH2Indexing.java:360)
org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.queryLocalSqlFields(IgniteH2Indexing.java:770)
org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:892)
org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:886)
org.apache.ignite.internal.util.lang.IgniteOutClosureX.apply(IgniteOutClosureX.java:36)
org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuery(GridQueryProcessor.java:1666)
org.apache.ignite.internal.processors.query.GridQueryProcessor.queryLocalFields(GridQueryProcessor.java:886)
org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:698)
com.test.ignite.cache.CacheWrapper.queryFields(CacheWrapper.java:1019)
The last line in my code executes a sql fields query as follows :
SqlFieldsQuery sql = new SqlFieldsQuery(queryString).setArgs(args);
cache.query(sql);
According to my understanding, Ignite has its own data grid which it uses to store the cache data and indices. It only makes use of H2 database to parse the SQL query and get a query execution plan.
But, the Thread dump shows that updates are being executed and transactions are involved. I don't understand the need for transactions or updates in a SQL Select Query.
I want to know the following about the role of H2 database in Ignite :
I went into the open source code of Apache Ignite(version 1.7.0) and saw that it was trying to open a connection to a specific schema in H2 database by executing the query SET SCHEMA schema_name ( connectionForThread() method of IgniteH2Indexing class ). Is one schema or one table created for every cache ? If yes, what information does it contain since all the data is stored in ignite's data grid.
I also came across another interesting thing in the open source code which is that Ignite tries to derive the schema name in H2 from space name ( reference can be found in queryLocalSqlFields() method of IgniteH2Indexing class ). I want to know what does this space name indicate and is it something internal to Ignite or configurable ?
Would the setting of schema and connection to H2 db happen for each of my SQL query, if yes then is there any way to avoid this ?
Yes, we call executeUpdate to set schema. In Ignite 2.x we will be able to switch to Connection.setSchema for that. Right now we create SQL schema for each cache and you can create multiple tables in it, but this is going to be changed in the future. It does not actually contain anything, we just utilize some H2 APIs.
Space name is basically the same thing as a cache name. You can configure SQL schema name for a cache using CacheConfiguration.setSqlSchema.
If you run queries using the same cache instance, schema will not change.
We have been running one server for the past few months and it contains all the files, SQL data, and is running as our server. We have recently bought 2 more servers to use replication because our database load was so high.
We are going to use a simple master slave replication using transaction replication in MSSQL however our methods that we use to acess LINQ entities must be changes.
For all functions that update they need to connect to the master, but all the ones that select need to query the slave.
How can we edit the connection string based on the function that needs to be done?
Any help would be appreciated.
Thanks
The simplest approach would be;
Create two connection strings on the web.config <connectionStrings> section for read and write.
When querying data, pass the read connection string name to the context's constructor.
and, pass the write connection string name when updating.
If you are using LINQ to entities, you can pass the connection string to the instance of the context i.e ModelContext ctx = new ModelContext("[edmx format connectionstring]");
We have a database where all connection strings are saved in a table. I need to use these connection strings to obtain data in their respective database. I can do it running queries but I want to use Linq and EF. How can I do it?
Thanks a lot.
Let me rephrase again, I am having access to database which will have a table containing multiple connection strings. I want to map database at runtime and then use it to retrieve the data. Is it possible?
ObjectContext exposes this constructor, which takes a connection string argument, so you can do:
ObjectContext yourContext = new ObjectContext(yourConnectionString);
It is not possible to map database at runtime. It will work only if your application knows mapping for every database and has all necessary classes prepared = you will have to create it in design time.