i just try to connect mongodb. but it show
Error # Database Connection Initialization : Error: database name
must be a string
Following is code:
try {
mongoDb.connect("localhost:27017/sample");
var db = mongoDb
}
catch (err) {
console.log('Error # Database Connection Initialization : ' + err);
}
Please try this
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/sample", function(err, database) {
if(err) return console.error(err);
db = database;
// the Mongo driver recommends starting the server here because most apps *should* fail to start if they have no DB. If yours is the exception, move the server startup elsewhere.
});
Related
When we set idle_in_transaction_session_timeout, the database will terminate connections that are idle for some time.
This works as expected, but I wonder how we should deal with this situation in the aplication code.
We are using pg-promise 10.3.1.
Details of the test:
we set the connection pool size to 1, so that we only have a single session
we set the for the idle-transaction-session-timeout to 2.5 sec:
SET idle_in_transaction_session_timeout TO 2500
now the active transaction will be in state idle in transaction:
see What can cause “idle in transaction” for “BEGIN” statements
now we start a transaction and sleep for 5 seconds
after 2.5sec the database will terminate the session and send an error to the client:
pgp-error error: terminating connection due to idle-in-transaction timeout
after another 2.5sec the transactional code tries to send a query (via the already terminated session), and this fails as expected:
dbIdle failed Error: Client has encountered a connection error and is not queryable
then pg-promise will try to rollback the transaction which will also fail (also expected, I guess)
But now we start a new query and also this query fails with
dbCall failed Client has encountered a connection error and is not queryable
is this expected? I was hoping that pg-promise can somehow remove the "broken" connection from the pool and that we could get a new one
obvously this is not the case, so how should we deal with this situation: i.e. how to recover, so that we can send new queries to the database?
Code example:
import pgPromise, { IMain } from "pg-promise";
import * as dbConfig from "./db-config.json";
import { IConnectionParameters } from "pg-promise/typescript/pg-subset";
const cll = "pg";
console.time(cll);
const pgp: IMain = pgPromise({
query(e) {
console.timeLog(cll,`> ${e.query}`);
},
error(e, ctx) {
console.timeLog(cll,"pgp-error", e);
}
});
const connectParams: IConnectionParameters = {
...dbConfig,
application_name: "pg-test",
max: 1
};
const db = pgp(connectParams);
/**
* #param timeoutMs 0 is no timeout
*/
async function setDbIdleInTxTimeout(timeoutMs: number = 0) {
await db.any("SET idle_in_transaction_session_timeout TO $1;", timeoutMs);
}
async function dbIdle(sleepTimeSec: number) {
console.timeLog(cll, `starting db idle ${sleepTimeSec}`);
const result = await db.tx(async t => {
await new Promise(resolve => setTimeout(resolve, sleepTimeSec * 1000));
return t.one("Select $1 as sleep_sec", sleepTimeSec);
});
console.timeLog(cll, result);
}
async function main() {
await setDbIdleInTxTimeout(2500);
try {
await dbIdle(5);
} catch (e) {
console.timeLog(cll, "dbIdle failed", e);
}
try {
await db.one("Select 1+1 as res");
} catch (e) {
console.timeLog(cll, "dbCall failed", e);
}
}
main().finally(() => {
pgp.end();
});
Console output (removed some useless lines):
"C:\Program Files\nodejs\node.exe" D:\dev_no_backup\pg-promise-tx\dist\index.js
pg: 23.959ms > SET idle_in_transaction_session_timeout TO 2500;
pg: 28.696ms starting db idle 5
pg: 29.705ms > begin
pg: 2531.247ms pgp-error error: terminating connection due to idle-in-transaction timeout
at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
name: 'error',
severity: 'FATAL',
code: '25P03',
}
pg: 2533.569ms pgp-error Error: Connection terminated unexpectedly
pg: 5031.091ms > Select 5 as sleep_sec
pg: 5031.323ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5031.489ms > rollback
pg: 5031.570ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5031.953ms dbIdle failed Error: Client has encountered a connection error and is not queryable
pg: 5032.094ms > Select 1+1 as res
pg: 5032.164ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5032.303ms dbCall failed Error: Client has encountered a connection error and is not queryable
Process finished with exit code 0
This issue #680 has been fixed in pg-promise 10.3.5
I use this code
But can anyone suggest me or guide me to a right document which can give me an example of how to connect to heroku postgress database with JDBC ?
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() +
dbUri.getPath();
connectionPool = new BasicDataSource();
if (dbUri.getUserInfo() != null) {
connectionPool.setUsername(dbUri.getUserInfo().split(":")[0]);
connectionPool.setPassword(dbUri.getUserInfo().split(":")[1]);
}
connectionPool.setDriverClassName("org.postgresql.Driver");
connectionPool.setUrl(dbUrl);
connectionPool.setInitialSize(1);
Connection connection = connectionPool.getConnection();
See heroku's documentation
Note:
The DATABASE_URL for the Heroku Postgres add-on follows the below convention
postgres://<username>:<password>#<host>:<port>/<dbname>
However the Postgres JDBC driver uses the following convention:
jdbc:postgresql://<host>:<port>/<dbname>?user=<username>&password=<password>
Sample code to parse DATABASE_URL into Jdbc format:
private static Connection getConnection() throws URISyntaxException, SQLException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
return DriverManager.getConnection(dbUrl, username, password);
}
There are two servers, my development server and the server Quick Books is currently installed on.
This is my .QWC file that is saved on the Quick Books server.
<?xml version="1.0" encoding="utf-8"?>
<QBWCXML>
<AppName>QBWebService</AppName>
<AppID></AppID>
<AppURL>https://<url>/soap.js</AppURL>
<AppDescription>This is The App</AppDescription>
<AppSupport>https://<url></AppSupport>
<UserName>User</UserName>
<OwnerID>{GUID}</OwnerID>
<FileID>{GUID}</FileID>
<QBType>QBFS</QBType>
</QBWCXML>
And this below my code in the soap.js file on my dev server.
var soap = require('soap'),
fs = require('fs'),
xml = fs.readFileSync('wsdl.wsdl', 'utf8'),
https = require('https'),
options = {
key: fs.readFileSync('/path/to/key'),
cert: fs.readFileSync('/path/to/crt'),
ca: [
fs.readFileSync('/path/to/crt', 'utf8'),
fs.readFileSync('/path/to/crt', 'utf8'),
fs.readFileSync('/path/to/crt', 'utf8')
]
};
var server = https.createServer(options, function(request, response) {
response.end("404: Not Found: " + request.url);
});
var myService = {
'QBWebConnectorSvc': {
'QBWebConnectorSvcSoap': {
authenticate: function(args) {
return {
authenticateResult: { string: [guid(), {}]}
};
}
}
}
};
server.listen(443, function() {
console.log('Listening Jimbo');
});
soap.listen(server, '/wsdl', myService, xml);
var thisUrl = './wsdl/wsdl.wsdl';
soap.createClient(thisUrl, function(err, client) {
if(err) {
console.log('Error soap create client:');
console.log(err);
}
else {
console.log('In soap create client else');
console.log('Client:');
console.log(client);
console.log('This should be describe');
client.setSecurity(new soap.ClientSSLSecurity('/Path/to/Server/key', '/path/to/server/crt', function(err, secure) {
if(err) {
console.log('Error Not Secure:');
}
else {
console.log('Secure');
console.log(secure);
client.QBWebConnectorSvc.QBWebConnectorSvcSoap.authenticate(function(err, done) {
if(err) {
console.log('Err Auth:');
console.log(err);
}
else {
console.log('Auth Done:');
console.log(done);
}
});
}
}));
The problem is that the Web Connector keeps saying that it cannot verify my certs, "QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.". I know the certs are good and the firewall is open between the two computers.
Below is from the WebConnector log file.
> 20160906.20:29:30 UTC : QBWebConnector.WebServiceManager.ReadQWC(QWCReader QWC) : Parsing application configuration xml file to load its content to variables
20160906.20:29:51 UTC : : QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.Certificate URL:https://<url>/soap.jsStackTrace:at System.Net.HttpWebRequest.GetResponse()
at QBWebConnector.QWCReader.CheckCertURL()Message (description of theexception):Unable to connect to the remote server Source (name of application or object that caused the exception):SystemTargetSite (method that threw the exception):System.Net.WebResponse GetResponse()InnerException:System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond <IP>:443
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
atSystem.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,IAsyncResult asyncResult, Exception& exception)
20160906.20:29:58 UTC :QBWebConnector.WebServiceManager.ReadQWC(QWCReader QWC) : QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.
QBWC1051: The new application was not added
20160906.20:29:58 UTC : : ~SingleInstanceHandler() - usingInstanceChannel = false. Returning without any Registry key delete or unmarshalling.
If you need any more information, please ask.
Any help would be much appreciated. Thank you.
I am creating a custom component which interfaces with MongoDB. I wrote a CoffeeScript file which just connects to MongoDB, and stored it at noflo/components folder.
MongoBase.coffee
noflo = require "noflo"
mongodb = require "mongodb"
url = require "url"
class exports.MongoBase extends noflo.Component
constructor: ->
super
#inPorts =
url: new noflo.Port()
#inPorts.url.on "data", (data) =>
try
#parseConnectionString(data)
#MongoClient = mongodb.MongoClient;
#MongoClient.connect #serverUrl, (err, db) ->
if err
console.log("Error in connecting to MongoDB")
else
console.log("Connected to MongoDB")
catch error
console.log(error)
parseConnectionString: (connectionString) =>
databaseUrl = try
url.parse(connectionString)
catch error
console.log(error)
[..., #serverUrl, #databaseName] = databaseUrl.split('/')
#serverUrl = "mongo://" + #serverUrl
I added the following entry to component.json
"MongoBase": "components/MongoBase.coffee"
In addition to this, I created a mongo.fbp file to check the flow of the component. The FBP file has the following code:
'mongodb://localhost:27017/test' -> url DocReader(MongoBase)
On running noflo mongo.fbp, I get the following error:
/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1628
edges.forEach(function (o, i) {
^
TypeError: Object #<Object> has no method 'forEach'
at Object.parser.registerEdges (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1628:15)
at peg$c25 (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:60:50)
at peg$parseline (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:749:30)
at peg$parsestart (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:282:12)
at Object.parse (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1650:18)
at Object.exports.loadFBP (/home/saurabh/workspace/noflo/lib/Graph.js:1065:33)
at /home/saurabh/workspace/noflo/lib/Graph.js:1116:24
at fs.js:268:14
at Object.oncomplete (fs.js:107:15)
Is there something wrong with my code, or the steps I am using to run the code?
You may have figured this out already, as it's been several month's since you asked, but I believe you need to add the getComponent() method to your class before you export it.
noflo = require "noflo"
mongodb = require "mongodb"
url = require "url"
class MongoBase extends noflo.Component
constructor: ->
super
#inPorts =
url: new noflo.Port()
#inPorts.url.on "data", (data) =>
try
#parseConnectionString(data)
#MongoClient = mongodb.MongoClient;
#MongoClient.connect #serverUrl, (err, db) ->
if err
console.log("Error in connecting to MongoDB")
else
console.log("Connected to MongoDB")
catch error
console.log(error)
parseConnectionString: (connectionString) =>
databaseUrl = try
url.parse(connectionString)
catch error
console.log(error)
[..., #serverUrl, #databaseName] = databaseUrl.split('/')
#serverUrl = "mongo://" + #serverUrl
MongoBase.getComponent = -> new MongoBase
exports.MongoBase = MongoBase
Additionally, in your graph for the component loader to work you need to specify the package your component lives in. If your package.json/component.json have a name entry like "name": "mongo-base" then you'd have to specify this in the FBP graph, like so:
'mongodb://localhost:27017/test' -> url DocReader(mongo-base/MongoBase)
N.B.: The loader clobbers any instances of 'noflo-' in the package name, so this needs to be taken into account. E.g. the name 'noflo-mongo' would get turned into just 'mongo', so when invoking the package's components you'd write in the fbp DocReader(mongo/MongoBase) and not DocReader(noflo-mongo/MongoBase).
Code:
var test_id = new ObjectID();
var gridStore = new GridStore(mongoose.connection.db, test_id , 'test_gs_getc_file' , 'w');
gridStore.open(function(err, gridStore){
var data = fs.readFileSync('./test.jpg');
gridStore.write(data, function(err, doc){
gridStore.close(function(err, doc){
GridStore.read(mongoose.connection.db, test_id , function(err, data){
if(err){
console.log("ERROR", err);
}else{
console.log("FILE data", data);
}
})
})
})
})
The file is getting saved in the fs.chunks and fs.files collections.
But when i try to read the file it gives Illegal chunk format error.
I tried
writeFile method, but it leads to same error
reinstalled mongodb, but it leads to same error
Complete Error message:
C:\Users\rajsubramanian\nodeapps\populate\node_modules\mongoose\node_modules\mon
godb\lib\mongodb\connection\server.js:594
throw err;
^
Error: Illegal chunk format
at Error (unknown source)
at new exports.Chunk (C:\Users\rajsubramanian\nodeapps\populate\node_modules
\mongodb\lib\mongodb\gridfs\chunk.js:43:11)
at nthChunk (C:\Users\rajsubramanian\nodeapps\populate\node_modules\mongodb\
lib\mongodb\gridfs\gridstore.js:582:24)
at Cursor.nextObject (C:\Users\rajsubramanian\nodeapps\populate\node_modules
\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:680:5)
at Cursor.nextObject.commandHandler (C:\Users\rajsubramanian\nodeapps\popula
te\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:658:14)
at Db._executeQueryCommand (C:\Users\rajsubramanian\nodeapps\populate\node_m
odules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1659:20)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (C:\Users\rajsubramanian\nodeapps\populate\node_
modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:410:25)
at Server.connect.connectionPool.on.server._serverState (C:\Users\rajsubrama
nian\nodeapps\populate\node_modules\mongoose\node_modules\mongodb\lib\mongodb\co
nnection\server.js:580:20)
DEBUG: Program node app exited with code 1