subsonic 3.0 how to correctly close connection? - subsonic

I'm using Subsonic 3.0.0.3 Active Directory.
During program work, opens many connections to MySQL data base. I can see this threads in MySQL Administrator.
I'm working with data base in this case:
var db = new DB();
var nClient = Enumerable.First(db.clients, c => c.id_client == cl.ID);
nClient.data = data;
nClient.Update();
another example
var nClient = new client { data= cl.data };
nClient.Save();
How should I correctly close connection to database? Or how to use only one connection to database?

It will close the connection straight way. SubSonic was designed to only open the connection when needed and close when finished.
If you want to use the same connection for several queries you need to look in to the batch query

Related

node-mongodb-native 2.X driver using to multiple databases

Is there a way to use multiple databases with a single connection to mongodb? I've found this:
https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#open
but as best I can tell those docs are old as there does not appear to be an open method on the MongoClient? Do you actually need to establish multiple connections?
Thanks!
Found it:
http://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#db
Here is their example
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
test.equal(null, err);
// Reference a different database sharing the same connections
// for the data transfer
var secondDb = db.db("integration_tests_2");
...
It is synchronous. Seems strange to me this method doesn't have the word "use" in it. Also seems strange it belongs to the db class. db.db('other_db').. a bit obscure. Did some tests, seems to work, so I'll mark this as the answer for anyone else that ends up here.

Error while executing a Ms Access db through nodejs

I am trying to access a Ms Access 2007 db trough nodejs in Windows 7, but even this simple query won't work. I receive the following message in the command prompt (this is a translation, the original is in portuguese): "Operation not allowed when object is closed". Anybody have any answers? The javascript code is written below:
var ADODB = require('node-adodb');
var connection = ADODB.open('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\teste\\dbteste.accdb;Persist Security Info=False;');
ADODB.debug = true;
connection
.query('SELECT * FROM [Tabela];')
.on('done', function (data){
console.log('Result:'.green.bold, data);
})
.on('fail', function (data){
});
Thanks!
Try to change the connectionString :
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\teste\\dbteste.accdb;Persist Security Info=False;
by
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\teste\\dbteste.accdb;Persist Security Info=False;
Also, check if the .accdb path is right.
Moreover, add ADODB.encoding = 'iso-8859-15'; for portuguese encoding.
I'm not sure but you may replace [Tabela] by simply Tabela. The ; at the sql query end is not necessary.
I encountered this issue too. This seems to be a bug in node-adodb module.(last seen at 8/31/2015)
In the linked page you can see that two people have encountered the same problem and the developer of node-adodb has asked them to upload the example( on 7/17/2015)

scope of callback in a .connect() (nodejs, mongodb)

im trying to use a global database object to handle the data on my nodejs server. It shall contain all data of all connected users. Ready to be accessed anytime out of ram.
Therefore i created a module:
function Database() {
var MongoClient = require('mongodb').MongoClient;
this.dbcon; //database connection
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/dbname", this.givedatabasevars);
};
As i want to reuse the database connection instead of opening new ones everytime i want to access the propertys of my object using the callback function and give them the connection object:
Database.prototype.givedatabasevars = function(err, getdbcon) {
if(err) {console.log(err)};
this.dbcon = getdbcon; //not working(scoped changed when used as callback?)
db.dbcom = getdbcon; //also not working(db object maybe not there yet as its a callback in constructor?)
};
I create the object using this globaly:
var Database = require('./node_modules/database/database.js');
var db = new Database();
Now im pretty new to nodejs and javascript all together. I tryed to get my head around how to access the objects scope, but from within the callback i cant even access the global scope.
Is this even possible?
Its the same for what to do with the data when using .find() with a callback. How do i get the data out of there to be used by my application? I need to store it, not to use it directly.
Regards,
Michael
You can access the global object using the global variable. Do console.log(this) on a plain file to see all the globals.
And for mongodb maybe mafintosh/mongojs can give you a more shareable interface. In fact I think with that you don't even need to have a global necessarily ;)

Setting the ACL when creating a new database via JS in XPages

I am creating a new database using JS from an action button. The following sequence happens:
When the database is created the -default- access is set to Manager which it has to be.
I then create several aclEntries. The first entry created for the current user and set them as a type person and having Manager rights
I then create several other entries with varying right and save the ACL.
If I open the Db the ACL looks and acts correctly except the -default- has an access level of Manager.
So I tried change my code to set the access level for -default- to Author in various ways, all resulting in an exception:
Change -default- to Author and save
First save the ACL as above, then change the -default- entry and save
create the ACL object newly using var newACL = db.getACL(), change and try to save
It seems that it does not recognize me as a manger in the ACL.
Q: How do I change the -default- access away from manager?
Probably the easiest way to get a good default ACL is using a template database. All entries in your template that come in square brackets are copied into the new database as ACL entries.
So in your template you would have
[-Default-]=Author and [Anonymous]=No Access which results in
-Default-=Author and Anonymous=No Access in the new DB
Update
Easier that it looks. You need to make sure to get the entries right...
Use this function:
function makeDB(dbName) {
var server = database.getServer();
var you = #UserName();
var dbDir = session.getDbDirectory(server);
var db = dbDir.createDatabase(dbName);
var acl = db.getACL();
acl.createACLEntry(you,6);
if (server != "" && server != you) {
acl.createACLEntry(server,6);
}
var def = acl.getEntry("-Default-");
def.setLevel(3);
acl.save();
}
Then you call the function using:
makeDB("someFancyDBName");
In the function we make sure that you, who runs the script and the server where it runs are both in the ACL as managers (including the case of a local database, where in the Notes client the server is empty and in the web preview would be your user name).
Works like a charm. If it doesn't work for you, there are a few things to check:
Does it work on local (no server involved)?
Do you have errors on the console?
What access level do you have in the server ECL
Check the "max Internet access" of the database
The previous answer (obsolete):
Other than that it is a little trickier...
You create the new database:
var db:Database = [... whatever you have todo here ...];
var acl:Acl = db.getAcl();
// Do whatever you do, don't touch -Default-
acl.save();
acl.recycle();
var dbURL = db.getUrl(); // <-- off my head might be slightly different name
db.recycle();
// Now the real work
var db2 = session.evaluate(dbUrl);
var acl2 = db2.getAcl();
// Change default here;
Typed off my head, contains typos.
This should work. Let us know how it goes

sqlite returns SQLITE_BUSY in WAL mode

I have a web application working with sqlite database.
My version of sqlite is the latest from official windows binary distribution - 3.7.13.
The problem is that under heavy load on database, sqlite API functions (such as sqlite3_step) are returning SQLITE_BUSY.
I pass the following pragmas when initializing a connection:
journal_mode = WAL
page_size = 4096
synchronous = FULL
foreign_keys = on
The databas is one-file database. And I'm using Mono 2.10.8 and Mono.Data.Sqlite assembly provided with it to access database.
I'm testing it with 50 parallel threads which are sending 50 subsequent http-requests each to my application. On every request some reading and writing are done to the database. Every set of IO operations is executed inside the transaction.
Everything goes well until near 400th - 700th request. In this (random) moment API functions are starting to return SQLITE_BUSY permanently (To be more exact - until the limit of retries is reached).
As far as i know WAL mode transparently supports parallel reads and writes. I've guessed that it could be because of attempt to read database while checkpoint operation is executed. But even after turning autocheckpoint off the situation remains the same.
What could be wrong in this situation?
How to serve large amount of parallel database IO correctly?
P.S.
Only one connection per request is supposed.
I use nhibernate configured with WebSessionContext.
I initialize my NHibernate session like this:
ISession session = null;
//factory variable is session factory
if (CurrentSessionContext.HasBind(factory))
{
session = factory.GetCurrentSession();
if (session == null)
CurrentSessionContext.Unbind(factory);
}
if (session == null)
{
session = factory.OpenSession();
CurrentSessionContext.Bind(session);
}
return session;
And on HttpApplication.EndRequest i release it like this:
//factory variable is session factory
if (CurrentSessionContext.HasBind(factory))
{
try
{
CurrentSessionContext.Unbind(factory)
.Dispose();
}
catch (Exception ee)
{
Logr.Error("Error uninitializing session", ee);
}
}
So as far as i know there should be only one connection per request life cycle. While proceessing the request, code is executed sequentially (ASP.NET MVC 3). So it doesn't look like any concurency is possible here. Can i conclude that no connections are shared in this case?
It's not clear to me if the request threads share the same connection or not. If they don't then you should not be having these issues.
Assuming that you are indeed sharing the connection object across multiple threads, you should use some locking mechanism as the the SqliteConnection isn't thread-safe (an old post, but the SQLite library maintained as part of Mono evolved from System.Data.SQLite found on http://sqlite.phxsoftware.com).
So assuming that you don't lock around using the SqliteConnection object, can you please try it? A simple way to accomplish this could look like this:
static readonly object _locker = new object();
public void ProcessRequest()
{
lock (_locker) {
using (IDbCommand dbcmd = conn.CreateCommand()) {
string sql = "INSERT INTO foo VALUES ('bar')";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
}
}
}
You may however choose to open a distinct connection with each thread to ensure you don't have any more threading issues with the SQLite library.
EDIT
Following-up on the code you posted, do you close the session after committing the transaction? If you don't use some ITransaction, do you flush and close the session? I'm asking since I don't see it in your code, and I see it mentioned in https://stackoverflow.com/a/43567/610650
I also see it mentioned on http://nhibernate.info/doc/nh/en/index.html#session-configuration:
Also note that you may call NHibernateHelper.GetCurrentSession(); as
many times as you like, you will always get the current ISession of
this HTTP request. You have to make sure the ISession is closed after
your unit-of-work completes, either in Application_EndRequest event
handler in your application class or in a HttpModule before the HTTP
response is sent.

Resources