I want to use an asynchronous connection to a postgres database for inserting a realtime incomming data. I am using Twisted for the TCP communication and i am giving txpostgres a shot for the interaction with the database.I am stacked with a weird message when i try to add a callback for my asynchronous insert.Here my code:
try:
conn = txpostgres.ConnectionPool(50,params)
d = conn.start()
def save_conn(c):
self.list_cnx.append(c)
print str(c),"Connect OK"
def print_err(m):
print m
d.addCallbacks(lambda _: save_conn(conn),lambda __: print_err('Connect NO'))
except Exception as e:
print "Cannot connect to database!!"
I am adding the refrence of the connction pool in a list for future query.
def insert_data(self,dic):
try:
insArRq="""INSERT INTO test_pool(date_msg, msg) VALUES ('%s','%s')"""%(dic['date'],dic['msg'])
for c in self.list_cnx:
def insert_finich():
print "insert finich"
def insert_error():
print "insert error"
d = c.runOperation(insArRq) # return a deferred as mentioned in the documentation
print d # for debug
d.addCallBack(insert_finich) # error mesage
except Exception as ee:
print "Insert error : ",ee
When i try to add a callback for the deferred returned by the runOperation this error appear:
<Deferred at 0x8d9782c waiting on Deferred at 0x8d9786c>
Insert error : Deferred instance has no attribute 'addCallBack'
and sometimes:
<Deferred at 0x8d97a0c>
Insert error : Deferred instance has no attribute 'addCallBack'
Help me please, i'am new to defrred concepts so i think i'am missing something.Thanks
The method is named addCallback; case is important.
Related
I have stored function in PostgreSQL, in which I have PL/PGSQL statement like this:
raise notice 'Message text';
I have also Groovy application, which uses default Sql class to call this function. I want to get messages (raise notice) from function are displayed in stdout or logged in my Groovy application.
I created PoC project to test this: https://github.com/lospejos/groovy-jdbc-get-server-messages
Please find comment in Groovy file: https://github.com/lospejos/groovy-jdbc-get-server-messages/blob/master/call_db_function.groovy
I also found this: https://stackoverflow.com/a/23087861/1828296
But I can't get how to get Statement object from Sql instance.
For the benefit of the others. To get server messages from stored function, call SQL like this:
def sql = Sql.newInstance('jdbc:postgresql://localhost/postgres', 'postgres', 'postgres')
final String paramValue = "Param value"
sql.query("select * from testme(param => :paramValue)", [paramValue: paramValue]) { resultSet ->
def rsRows = [:]
while (resultSet.next()) {
rsRows << resultSet.toRowResult()
}
def warning = resultSet.getStatement().getWarnings()
while (warning) {
println "[${LocalDateTime.now()}] [${warning.getSQLState()}] ${warning.message}"
warning = warning.nextWarning
}
println rsRows
}
I also updated repository code.
I am getting an error msg is not defined when trying to loop through different OBX segments. In my destination DB Writer I have the code var msg = channelMap.get('msg'); but how to store the msg in the transformer with channelMap.put('msg', msg)?
This is what I have currently in the transformer(javascript mapper):
var message = message.getRawData();
channelMap.put('msg', message);
In destination DB writer:
var msg = channelMap.get('msg');
Error:
TypeError: Cannot call method "getRawData" of undefined;
On the source connector filters and transformers, msg is a given variable, you can't (and should not) declare it (var msg) and is not on the channelMap to be able to get it (channelMap.get('msg')), you just use it: msg[...].
On the destination you can get the message without putting it on the channelMap.
I need to run a raw sql query, but I'm getting an error when I try to open the connection to the database. "The connection was not closed. The connection's current state is open."
_loginValidator and _contactService are passed into the controller through DI:
services.AddScoped<ILoginValidator, LoginValidator>();
services.AddScoped<IContactService, ContactService>();
The two lines below are in an action function of the controller. If I switch the two lines, the error goes away...:
var validationErrors = _loginValidator.Validate(id, "");
var user = _contactService.GetContact(id);
Here is _loginValidator.Validate. If I comment out the second line, the error goes away...:
public LoginValidationResult Validate(int userId, string encryptedPassword)
{
var vr = new LoginValidationResult();
var user = _context.Users.Include(u => u.LoginUserQuestionAnswers).FirstOrDefault(u => u.Id == userId);
//...
}
Here is _contactService.GetContact. This is where I get the error:
public ContactDto GetContact(int id)
{
var conn = _context.Database.GetDbConnection();
//ERROR HERE!!!
conn.Open();
//work on conn, for example: ExecuteReader
conn.Close();
}
Notes:
If I comment out the _context line in the Validate(...) function, I do not get the error.
If I switch the two lines I listed in the action function, I do not get the error.
I think the problem is that EntityCore is not closing the connection after I finish using it in _loginValidator.Validate(...)
Anyone know how I can deal with this problem?
DB Connection is an unmanaged resource and you need to close it yourself. The best practice is to use a using statement for your DB connections.
See these links:
http://stackoverflow.com/questions/35077000/entity-framework-7-get-database-time
https://msdn.microsoft.com/en-us/data/dn456849.aspx
The connection being left open after the FirstOrDefault query is a bug. I filed https://github.com/aspnet/EntityFramework/issues/6581 for it and we just triaged it for the 1.0.2 release.
To workaround the bug for now I think you can check if the connection is already open and, if so, don't try to open it again.
Looks like asyncio is the module to use. I'll leave this question up anyway, because it doesn't look like there is a way to catch specific errors with asynchat.
class mysocket(asynchat.async_chat):
terminator = b'\n'
def __init__(self,sock=None):
asynchat.async_chat.__init__(self,sock)
self.create_socket()
# Try always succeeds with self.connect
try:
self.connect(('badhost',6667))
print('command always successful')
except:
print('This never gets printed')
How do I catch errors from the self.connect() method that causes an uncaught exception.
error: uncaptured python exception, closing channel <main.mysocket badhost:6667 at 0x7f0a03e66a58> (:[Errno 111] Connection refused [/usr/lib/python3.4/asyncore.py|read|83] [/usr/lib/python3.4/asyncore.py|handle_read_event|439] [/usr/lib/python3.4/asyncore.py|handle_connect_event|447])
All that is left to try is overwrite the handle_connect_event() method and put asyncore.handle_connect_event(self). I would like to get a professional answer to this dilemma.
Try to override default handle_error method:
def handle_error(self):
t, v, tb = sys.exc_info()
if t == socket.error:
# do smth with it
print("Connect error")
I am using Motor driver to connect to Mongo DB.
Below is the code to insert data to the collection
client = motor.MotorClient('mongodb://localhost:27017').open_sync()
conn = client['database']['collection']
result = conn.insert({'foo': 'bar'})
print 'result:', result
The insert statement always returns None.
This is not a Tornado application.
Can motor be only used with Tornado?
If not why is the insert returning none?
You use motor just like pymongo. But motor is asynchronous: it means that when your print is executed, maybe the db request is not finished yet.
Furthermore, motor insert does not return anything, and you need to use a callback function as second argument with it. Cf. the differences between pymongo and motor, and the motor tutorial on how to insert a document.
In your case, the good way of soing this would be:
client = motor.MotorClient('mongodb://localhost:27017').open_sync()
conn = client['database']['collection']
result = conn.insert({'foo': 'bar'}, callback=once_done)
def once_done(result, error):
if error: print 'error:', error
else:
print 'result:', result
I guess, the WriteConcern is not set from the client-driver.
If you set it as safe=true, then you will get the status of the insert operation. Otherwise with safe=false, the insert operation is fire and forget.
You can try:
motor.MotorClient('mongodb://localhost:27017/?safe=true')