redbeans use existing database connection - redbean

Is there any easy way to use a existing database connection with Redbeans. E.g. if I first connect using another PDO database class, and then I want Redbeans to use this open connection? I am primarily using MySQL.

Well, it actually is quite easy:
Just connect like this to a PDO database, e.g.
$dbh = new PDO(
$url,
$username,
$password,
$options
);
And then:
R::setup($dbh);
Then you use the same connection.

Related

Correct way to use DbConnection, DbTransaction with connection pooling, transactionScope and dependency injection?

I have a Oracle database and I'm using the Oracle.ManagedDataAccess.
In some cases I will need to do actions in a single transactions, but often not.
I'm not sure what the best way to handle DbConnection objects within a single TransactionScope.
I could inject a DbConnection into the repositories and then even use LifetimePerScope to ensure they all get the same DbConnection instance. But is that a smart move, is it ok to .Open() the connection once.
using (var scope = _lifetimeScope.BeginLifetimeScope())
{
var connection = scope.Resolve<IDbConnection>();
var personRepo = scope.Resolve<IPersonRepository>();
var workRepo = scope.Resolve<IWorkRepository>();
connection.Open();
var transaction = connection.BeginTransaction()
personRepo.DeleteById(someId);
workRepo.DeleteByPersonId(someId);
transaction.Commit();
}
This would force me to always use a LifetimeScope, even if not using a Transaction, and open the connection outside the repository method.
Are TransactionScopes dependent on a single connection or can I open multiple connections (how does the connectionPool handle that while a transaction is open?) within the same transaction?
I'm a total outsider to DbConnections and all that so I might be totally misunderstanding the best way to use TransactionScope and DbConnections.
Possible duplicate of: Why always close Database connection?
Since this has a bounty, I can't flag it as a duplicate :(
Anyway, connection pooling is largely done for you. You should close connections as soon as you can, to return them to the pool.
Transactions are related to a specific open connection and should be finished when closing the connection.
TransactionScope related to BeginTransaction() is specific to a connection.
If you want to maintain a transaction across multiple connections(multiple DBs,resources), then you need DTC aware TransactionScope. Here is a similar SO post. You need to use Oracle.ManagedDataAccessDTC.dll to facilitate that.
You might want to go through these links:
1.All about transactionscope
2.How To Configure DTC to Support Oracle Transactions
Hope this helps.

Pass vijava ServiceInstance via rabbitmq or another task queue

I'm trying to create a system where a master will create a connection to vcenter and passes the serviceinstance object to a bunch of performance collectors that can then do their work and exit. My question is what would be the best method to share the SI object? I was thinking of using a messaging queue for the purpose, but I'm not really keen on serializing objects. Is there any other more efficient way?
That SI is only going to work on that vCenter which created the SI. If thats is not going to be a problem for you then simply place the session id on the bus for your workers to pick up then they should be able to create a new SI using the session id.
The first time you connect:
ServiceInstance serviceInstance = new ServiceInstance(new URL("https://vcenter/sdk"),user, passwd, true);
String sessionId = serviceInstance.getServerConnection().getSessionStr();
Next place that sessionId on the bus. Have your worker pick it up and do:
ServiceInstance si2 = new ServiceInstance(new URL("https://vcenter/sdk"), sessionId, true);
The default timeout for that session is like 30 mins IIRC..
Also a little self plugging I would suggest a move from vijava to yavijava. Its a fork I maintain which has added lots of nifty features, and Im even currently adding 6.0 support. https://github.com/yavijava/yavijava

How can I use oAuth to secure individual databases within RavenDB?

I'm looking to use a single RavenDB server to host multiple databases for multiple applications. There won't be many databases (maybe 3 or 4), but I'd like to secure each of them individually
I'm looking at the Docs, and I'm wondering if this security approach will work on a per/db, and if there's anything special I need to do?
store.DatabaseCommands.Put("Raven/ApiKeys/sample",
null,
RavenJObject.FromObject(new ApiKeyDefinition
{
Name = "sample",
Secret = "ThisIsMySecret",
Enabled = true,
Databases = new List<DatabaseAccess>
{
new DatabaseAccess {TenantId = "*"},
new DatabaseAccess {TenantId = Constants.SystemDatabase},
}
}), new RavenJObject());
You can certainly provide access to just a specific database, yes.
Just have only that db in the Databases collection, and ti will work.
You'll probably want to have separate API Keys for each database, of course.

Grails - how to connect to another database during an action

What I need to do is this (either in an action or a service):
someAction() {
// connect to database
def otherDatasource = new Datasource(otherOptions)
if (otherDatasource.isOnline()) {
def list = ExclusiveDomainFromOtherDatasource.list()
// do stuff with the data...
otherDatasource.close()
}
}
I'm not using datasources plugin because the other database may be offline and the app connects during first-run, or maybe I'm forgetting something.
I know I could use some basic jdbc library and make a raw sql for getting the data, but that isn't very groovish, is this the only way? or is there a plugin that allow me to do that?
Unless you're using some sort of custom DataSource, there is no isOnline() method, so you might as well just use the DataSources plugin.
Actually, newer versions of grails have multiple-datasource support built-in, so you don't need the plugin.
I think that you're just going to have to try to get a connection from the DataSource, and be prepared to catch and handle the exception that you'll get if the database is offline.
A well-configured database conneciton pool should allow you to start connecting successfully once the database comes online.

Connection Status of SQLite in IOS

Is there a way one can check the connection status of a SQLite DB in IOS. I do not want to keep opening the db connection again and again. As a work around, I have put the SQLite DB object into a static variable and check if the object is NIL, else use the object as is.
Is there a simpler and cleaner way to do this
A lot of SQLite Wrappers for iOS provide this functionality. Here is a library I wrote for handling SQLite connections which you can use to check the status of a connection: https://github.com/ziminji/objective-c-sql-query-builder
First of all, I would recommend using FMDB instead of SQLite directly.
To answer your question: don't bother. I have many apps, with many users and I have never seen the database "connection" fail. It just doesn't fail, it's not a network connection, just an open file.
Try www.github.com/pmurphyjam/DBExample It's an Xcode project that uses SQLite. It abstracts the entire SQL layer for you so you can concentrate on just writing SQL queries. It does large transactions also. The SQL syntax is exactly like FMDB, and it also uses Dictionaries for complex queries. Here's an example:
For selects NSMutableArray = GetRecordsForQuery:#"select firstName, lastName from Company where lastName = ? ",#"Smith",nil];
OR For inserts, deletes or updates
BOOL = ExecuteStatement:#"insert into Company (firstName, lastName) values(?,?)",#"John",#"Smith", nil];
There is an example project for using SQLite here you can refer to: https://github.com/AaronBratcher/ABSQLite
It has classes for accessing SQLite in a more traditional database way.

Resources