Executing javascript in Python

Recently I was trying to execute a piece of javascript in Python. I found a very good library which helps in doing it.

PyV8 can be easily used to execute javascript code in python and then get the result in a python variable.

Example:

import PyV8


context = PyV8.JSContext()
context.enter()
result = context.eval("3*6")
resultInPython = PyV8.convert(result)

Creating executable jars and executing it

Creating Executable jars
Executable jars can be created by following these steps:

1) Write your java source code in a .java file and compile it. Lets say for example it is Test.java and compiled code is in Test.class

2) Create a manifest.txt file.
The manifest file should look something like this:

Main-Program: com.company.name.Test

NOTE: Make sure to keep a newline character at the end of the text.

Save the file. The folder hierarchy must be like this:

temp/bin/com/company/name/*.class
temp/bin/manifest.txt

3) Create the jar file by executing this command:

jar -cvfm test.jar manifest.txt com/company/name/*.class

Executing the Jar:
To execute the jar execute the following command:

java -jar test.jar commandlineparams

 

Connection pooling with C3P0 for MySQL

Connection pooling is the method by which a pool of database connections are open to be used. This helps in reusing the resource and not wasting time in opening and closing new connections every time the application wants to make a request to the database server. Connection pooling is managed by the client side rather than the server side. Connection pooling can also be configured in the application server if it is a web application otherwise we can use an external library for pooling.

MySQL Connection pooling:

For MySQL, one way to open a connection pool is to use the C3P0 JAVA library. The library provides methods to monitor and modify the connection resources. A very simple implementation of the C3P0 library is as follows:


ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" );
cpds.setJdbcUrl( "jdbc:mysql://"+hostName+":"+portNo+"/"+DBName );
cpds.setMaxPoolSize(50);
cpds.setMinPoolSize(10);
cpds.setUser("dbuser");
cpds.setPassword("dbpassword");
Connection con = cpds.getConnection();

Now this Connection object can be used to do the MySQL queries as usual.

In Cassandra this can be achieved by doing the following:


CassandraHostConfigurator cassandraConfig = new CassandraHostConfigurator(host+":"+portNo);
cassandraConfig.setMaxActive(50);
//Now we can create a cluster object with this config object
Cluster c = HFactory.getOrCreateCluster(cluster,cassandraConfig);

In MongoDB this can be done by:

MongoOptions op = new MongoOptions();
op.connectionsPerHost = 50;
m = new Mongo(hostName+":"+portNum,op);