Subject: [Pljava-dev] cannot set the classpath to a specific jar to get an external connection

From: fluca1978 at infinito.it (Luca Ferrari)
Date: Thu, 3 Jun 2010 10:52:32 +0200

Hi all,
not sure if what I'd like to do is possible, but I'd like to use JDBC within a 
pl/java function to extract data from another database (something similar to 
dbilink). However if I try to change the connection to another URL rather that 
"jdbc:default:connection" I got an error saying there is no suitable driver, 
so I tried to add explicitly the postgresql jar to the schema, but the 
following fails:

 select sqlj.set_classpath('public', 
'/java/jars/postgresql-8.4-701.jdbc4.jar');
ERROR:  java.sql.SQLException: No such jar: 
/java/jars/postgresql-8.4-701.jdbc4.jar

So my questions are:
1) what am I missing in setting the classpath?
2) is it possible to open a connection to another database cluster?

Thanks,
Luca

---

From: Jon.Roberts at asurion.com (Roberts, Jon)
Date: Thu, 3 Jun 2010 07:15:30 -0500

I think you need to first install the jar and then set the class path.

For example:
select * from
sqlj.install_jar('file:///java/jars/postgresql-8.4-701.jdbc4.jar',
'example_jar', true);
select sqlj.set_classpath('public', 'example_jar');

I use PL/Java to connect to other databases (Oracle, SQL Server, and
PostgreSQL) but I created a single Jar with all of my drivers and load
that to the database.

Jon

-----Original Message-----
From: pljava-dev-bounces at pgfoundry.org
[mailto:pljava-dev-bounces at pgfoundry.org] On Behalf Of Luca Ferrari
Sent: Thursday, June 03, 2010 3:53 AM
To: pljava-dev at pgfoundry.org
Subject: [Pljava-dev] cannot set the classpath to a specific jar to get
anexternal connection

Hi all,
not sure if what I'd like to do is possible, but I'd like to use JDBC
within a 
pl/java function to extract data from another database (something
similar to 
dbilink). However if I try to change the connection to another URL
rather that 
"jdbc:default:connection" I got an error saying there is no suitable
driver, 
so I tried to add explicitly the postgresql jar to the schema, but the 
following fails:

So my questions are:

Thanks,
Luca
_______________________________________________
Pljava-dev mailing list
Pljava-dev at pgfoundry.org
http://pgfoundry.org/mailman/listinfo/pljava-dev

---

From: fluca1978 at infinito.it (Luca Ferrari)
Date: Thu, 3 Jun 2010 14:50:09 +0200

On Thursday 03 June 2010 02:15:30 pm Roberts, Jon's cat walking on the 
> select sqlj.set_classpath('public', 'example_jar');

Yes, I've installed the jar before ( I did not mention) and then I was able to 
set the classpath using the jar alias. However, keeping the jar separated or 
merging them into a single jar does not solve the problem and the error still 
is:

ERROR:  java.sql.SQLException: No suitable driver found for 
jdbc:postgresql://localhost/myDb

and in the logs I've got the stacktrace:

java.sql.SQLException: No suitable driver found for 
jdbc:postgresql://localhost/myDB
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at itpug.pljava.RowGenerator.getResultSet(RowGenerator.java:40)
        at 
org.postgresql.pljava.internal.ResultSetPicker.<init>(ResultSetPicker.java:25)

Summarizing this is what I've done:
1) sqlj.replace_jar to my new jar containing the Java client
2) sqlj.install_jar for the postgresql driver jar
3) set the classpath so that now I've got:
select sqlj.get_classpath('public');
 get_classpath 
---------------
 driver:myJar
(1 row)

with driver the jar for the jdbc driver, and myjar the jar of my client.

Any suggestion?

Luca

---

From: Jon.Roberts at asurion.com (Roberts, Jon)
Date: Thu, 3 Jun 2010 07:56:54 -0500

How are you connecting?  Is it something like this:

		Class.forName("org.postgresql.Driver");

		String connectionUrl = "jdbc:postgresql://" + server +
":" + port + "/" + database;

		conn = DriverManager.getConnection(connectionUrl,
userName, password);

Jon

-----Original Message-----
From: Luca Ferrari [mailto:fluca1978 at infinito.it] 
Sent: Thursday, June 03, 2010 7:50 AM
To: Roberts, Jon
Cc: pljava-dev at pgfoundry.org
Subject: Re: [Pljava-dev] cannot set the classpath to a specific jar to
get anexternal connection

> select sqlj.set_classpath('public', 'example_jar');

Yes, I've installed the jar before ( I did not mention) and then I was
able to 
set the classpath using the jar alias. However, keeping the jar
separated or 
merging them into a single jar does not solve the problem and the error
still 
is:

org.postgresql.pljava.internal.ResultSetPicker.<init>(ResultSetPicker.ja
va:25)

Any suggestion?

Luca

---

From: fluca1978 at infinito.it (Luca Ferrari)
Date: Thu, 3 Jun 2010 15:39:14 +0200

On Thursday 03 June 2010 02:56:54 pm Roberts, Jon's cat walking on the 
> 		conn = DriverManager.getConnection(connectionUrl,

Using either reflection or direct instantation I've got a new exception:

java.lang.NoClassDefFoundError: Could not initialize class 
sun.security.provider.NativePRNG
        at sun.security.provider.Sun.<init>(Sun.java:91)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
Method)
        at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at java.lang.Class.newInstance0(Class.java:355)
        at java.lang.Class.newInstance(Class.java:308)
        at sun.security.jca.ProviderConfig$3.run(ProviderConfig.java:240)
        at java.security.AccessController.doPrivileged(Native Method)
        at 
sun.security.jca.ProviderConfig.doLoadProvider(ProviderConfig.java:225)
        at sun.security.jca.ProviderConfig.getProvider(ProviderConfig.java:205)
        at sun.security.jca.ProviderList.getProvider(ProviderList.java:215)
        at sun.security.jca.ProviderList.getService(ProviderList.java:313)
        at sun.security.jca.GetInstance.getInstance(GetInstance.java:140)
        at java.security.Security.getImpl(Security.java:659)
        at java.security.MessageDigest.getInstance(MessageDigest.java:129)
        at org.postgresql.util.MD5Digest.encode(MD5Digest.java:46)
        at 
org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:339)
        at 
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
        at 
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
        at 
org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
        at 
org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
        at 
org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
        at 
org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
        at 
org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
        at org.postgresql.Driver.makeConnection(Driver.java:393)
        at org.postgresql.Driver.connect(Driver.java:267)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at itpug.pljava.RowGenerator.getResultSet(RowGenerator.java:43)
        at 
org.postgresql.pljava.internal.ResultSetPicker.<init>(ResultSetPicker.java:25)

The code I use is the following:

 public ResultSet getResultSet() throws SQLException {
	
	    org.postgresql.Driver driver = new org.postgresql.Driver();
	
	Connection connection = DriverManager.getConnection( this.databaseName, 
this.username, this.password );
	Statement statement   = connection.createStatement();
	return statement.executeQuery(" SELECT * FROM "+  this.tableName );
    }

Any idea?

Thanks,
Luca

---

From: fluca1978 at infinito.it (Luca Ferrari)
Date: Thu, 3 Jun 2010 15:43:21 +0200

On Thursday 03 June 2010 03:39:14 pm Luca Ferrari's cat walking on the 
> sun.security.provider.NativePRNG

I've noted that when the function is called the first time the logs are 
differents and provide the following stacktrace:

Caused by: java.lang.SecurityException: read on /dev/random
        at 
org.postgresql.pljava.internal.Backend$1.assertPermission(Backend.java:176)
        at 
org.postgresql.pljava.internal.Backend$PLJavaSecurityManager.nonRecursiveCheck(Backend.java:111)
        at 
org.postgresql.pljava.internal.Backend$PLJavaSecurityManager.checkPermission(Backend.java:89)
        at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
        at java.io.File.exists(File.java:731)
        at sun.security.provider.NativePRNG$1.run(NativePRNG.java:60)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.security.provider.NativePRNG.initIO(NativePRNG.java:57)
        at sun.security.provider.NativePRNG.<clinit>(NativePRNG.java:54)
        ... 31 more
2010-06-03 15:39:43 CEST ERROR:  java.lang.ExceptionInInitializerError

so it seems a problem of reading /dev/random, even if the user has the 
permissions:

$ ls -l /dev/random 
crw-rw-rw- 1 root root 1, 8 2010-06-03 15:31 /dev/random

Any idea?

Thanks,
Luca

---

From: thomas at tada.se (Thomas Hallgren)
Date: Thu, 03 Jun 2010 16:32:41 +0200

PL/Java is implemented as a trusted procedure language. It will 
therefore prevent all access to the filesystem. You can use language 
"javaU" if you want untrusted. More details here: 
http://wiki.tada.se/index.php/Security.

Regards,
Thomas Hallgren

---

From: fluca1978 at infinito.it (Luca Ferrari)
Date: Fri, 4 Jun 2010 10:42:18 +0200

On Thursday 03 June 2010 04:32:41 pm Thomas Hallgren's cat walking on the 
> http://wiki.tada.se/index.php/Security.

Thanks, it solved the problem!

Luca