Hi Ishara,
When creating a Database object, you have to pass the JDBC URL and the
database credentials as follows.
var db = new Database("jdbc:h2:tmp/demo", "admin", "admin");
Please follow the Jaggery documentation on Databases at [1]
[1] http://jaggeryjs.org/documentation.jag?api=rdb
Regards,
Dunith
On Fri, Aug 29, 2014 at 10:52 AM, Ishara Cooray <[email protected]> wrote:
> my function is as follow and NPE is thrown @ db = new Database("db")
>
> var executeQuery = function(queryString) {
> var results ;
> var db = null;
> try{
>
> db = new Database("db");// error here
>
> results = db.query(queryString);
> } catch (e) {
> log.error(e);
> throw "Error while accessing database
> " + " : " + e.message;
> }finally{
> if(db!=null){
> db.close();
> log.info("db closed");
> }
> }
> return results;
> }
>
> Ishara Cooray
> Senior Software Engineer
> Mobile : +9477 262 9512
> WSO2, Inc. | http://wso2.com/
> Lean . Enterprise . Middleware
>
>
> On Thu, Aug 28, 2014 at 6:48 PM, Ishara Cooray <[email protected]> wrote:
>
>> I have a function which does
>> 1. create Database connection
>> 2. execute query
>> 3. close the db connection
>>
>>
>> var executeQuery = function(queryString) {
>> var results ;
>> var db = null;
>> try{
>> var db = new Database("db");
>> results = db.query(queryString);
>> } catch (e) {
>> log.error(e);
>> throw "Error while accessing
>> database " + " : " + e.message;
>> }finally{
>> if(db!=null){
>> db.close();
>> log.info("db closed");
>> }
>> }
>> return results;
>> }
>>
>> i use this function when a sql is needed to be executed.
>> But in some cases, when try to create new Database connection it gives
>> below NullPointer Exception
>>
>> [2014-08-28 18:19:46,849] ERROR {JAGGERY.modules.database.dataaccess:jag}
>> - org.mozilla.javascript.WrappedException: Wrapped
>> java.lang.NullPointerException
>> (/cloudmgt/modules/database/dataaccess.jag#21)
>> at
>> org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
>> at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
>> at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:392)
>> at
>> org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:343)
>> at
>> org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2349)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.database.c0._c_anonymous_1(/cloudmgt/modules/database/dataaccess.jag:21)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.database.c0.call(/cloudmgt/modules/database/dataaccess.jag)
>> at
>> org.mozilla.javascript.ScriptRuntime.applyOrCall(ScriptRuntime.java:2430)
>> at
>> org.mozilla.javascript.BaseFunction.execIdCall(BaseFunction.java:269)
>> at
>> org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.database.c2._c_anonymous_2(/cloudmgt/modules/database/module.jag:8)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.database.c2.call(/cloudmgt/modules/database/module.jag)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0._c_getTenantDisplayName_3(/cloudmgt/modules/tenant/users/add/add.jag:63)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0.call(/cloudmgt/modules/tenant/users/add/add.jag)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0._c_anonymous_6(/cloudmgt/modules/tenant/users/add/add.jag:132)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0.call(/cloudmgt/modules/tenant/users/add/add.jag)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0._c_sendUserInvite_8(/cloudmgt/modules/tenant/users/add/add.jag:213)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.users.add.c0.call(/cloudmgt/modules/tenant/users/add/add.jag)
>> at
>> org.mozilla.javascript.ScriptRuntime.applyOrCall(ScriptRuntime.java:2430)
>> at
>> org.mozilla.javascript.BaseFunction.execIdCall(BaseFunction.java:269)
>> at
>> org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.c0._c_anonymous_8(/cloudmgt/modules/tenant/module.jag:26)
>> at
>> org.jaggeryjs.rhino.cloudmgt.modules.tenant.c0.call(/cloudmgt/modules/tenant/module.jag)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0._c_anonymous_1(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag:82)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0.call(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag)
>> at
>> org.mozilla.javascript.optimizer.OptRuntime.call0(OptRuntime.java:23)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0._c_script_0(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag:6)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0.call(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag)
>> at
>> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
>> at
>> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0.call(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag)
>> at
>> org.jaggeryjs.rhino.cloudmgt.site.blocks.tenant.users.add.ajax.c0.exec(/cloudmgt/site/blocks/tenant/users/add/ajax/add.jag)
>> at
>> org.jaggeryjs.scriptengine.engine.RhinoEngine.execScript(RhinoEngine.java:570)
>> at
>> org.jaggeryjs.scriptengine.engine.RhinoEngine.exec(RhinoEngine.java:273)
>> at
>> org.jaggeryjs.jaggery.core.manager.WebAppManager.execute(WebAppManager.java:432)
>> at
>> org.jaggeryjs.jaggery.core.JaggeryServlet.doPost(JaggeryServlet.java:29)
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>> at
>> org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
>> at
>> org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
>> at
>> org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
>> at
>> org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
>> at
>> org.jaggeryjs.jaggery.core.JaggeryFilter.doFilter(JaggeryFilter.java:21)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>> at
>> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
>> at
>> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
>> at
>> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
>> at
>> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
>> at
>> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
>> at
>> org.wso2.carbon.tomcat.ext.valves.CompositeValve.continueInvocation(CompositeValve.java:178)
>> at
>> org.wso2.carbon.tomcat.ext.valves.CarbonTomcatValve$1.invoke(CarbonTomcatValve.java:47)
>> at
>> org.wso2.carbon.webapp.mgt.TenantLazyLoaderValve.invoke(TenantLazyLoaderValve.java:56)
>> at
>> org.wso2.carbon.tomcat.ext.valves.TomcatValveContainer.invokeValves(TomcatValveContainer.java:47)
>> at
>> org.wso2.carbon.tomcat.ext.valves.CompositeValve.invoke(CompositeValve.java:141)
>> at
>> org.wso2.carbon.tomcat.ext.valves.CarbonStuckThreadDetectionValve.invoke(CarbonStuckThreadDetectionValve.java:156)
>> at
>> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
>> at
>> org.wso2.carbon.tomcat.ext.valves.CarbonContextCreatorValve.invoke(CarbonContextCreatorValve.java:52)
>> at
>> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
>> at
>> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
>> at
>> org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
>> at
>> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
>> at
>> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1653)
>> at
>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
>> at
>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
>> at java.lang.Thread.run(Thread.java:722)
>> Caused by: java.lang.NullPointerException
>> at
>> org.jaggeryjs.hostobjects.db.DatabaseHostObject.jsConstructor(DatabaseHostObject.java:87)
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>> at java.lang.reflect.Method.invoke(Method.java:601)
>> at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
>> ... 72 more
>>
>>
>> Is there a way to synchronize the function in Jaggery or any other
>> suggestion to get rid from this error?
>>
>> Any help would be appreciated.
>>
>> Thanks.
>> Ishara Cooray
>> Senior Software Engineer
>> Mobile : +9477 262 9512
>> WSO2, Inc. | http://wso2.com/
>> Lean . Enterprise . Middleware
>>
>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
Regards,
Dunith Dhanushka,
Senior Software Engineer - BAM,
WSO2 Inc,
Mobile - +94 71 8615744
Blog - dunithd.wordpress.com <http://blog.dunith.com>
Twitter - @dunithd <http://twitter.com/dunithd>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev