Re: BasicDataSource creation throws SQLException

2016-08-12 Thread Roy Leonardus
Hi Christopher,

I only called the basicdatasource.getconnection() in the main test class.

but i added TestDriver.addconnection() beforehand.

this is my driver class

public class TestDriver implements Driver {

public static final String TESTDRIVER_PREFIX = "jdbc:not_a_db:";

static {
try {
DriverManager.registerDriver(new TestDriver());
} catch (SQLException e) {
e.printStackTrace();
}
}

private static List connections = new ArrayList();

public static void reset() {
connections.clear();
}

public static void addConnection(Connection c) {
connections.add(c);
}

private static Connection getNextConnectionFromList() {
if (connections.isEmpty()) {
throw new IllegalStateException("No more connections available.");
}
return connections.remove(0);
}


@Override
public Connection connect(String url, Properties info) throws SQLException {
return getNextConnectionFromList();
}

@Override
public boolean acceptsURL(String url) throws SQLException {
return url.contains(TESTDRIVER_PREFIX);
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
return new DriverPropertyInfo[0];
}

@Override
public int getMajorVersion() {
return 3;
}

@Override
public int getMinorVersion() {
return 42;
}

@Override
public boolean jdbcCompliant() {
return true;
}

// for JDK 7
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}

as you can see, there is no resultset returned.

regarding the validation query, i only use it during the test with Tomcat
8, as it complains about it.

I agree with the mocking connection, the biggest PITA is : it does not
prove anything, that is why i am using Derby to speed up the process.

What John Huss wrote in the email chain is also interesting and need to
keep in note, though.

Roy


On Thu, Aug 11, 2016 at 6:02 PM, Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> Roy,
>
> On 8/11/16 3:43 AM, Roy Leonardus wrote:
> > Hello Christopher,
> >
> > maybe i'm a little bit slow here, but from what i see, we should
> > mock the connection method
> >
> > Connection connection= ds.getConnection();
> >
> > something like
> >
> > Connection connection =(Connection) EasyMock.expect(ds.
> > isvalid()).andReturn(true).anyTimes();
> >
> > Problem is, the error happened during the validation of the
> > PoolableConnection, below is the build error from the surefire
> >
> > Caused by: java.sql.SQLException: isValid() returned false at
> > org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConne
> ction.java:283)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnect
> ion(PoolableConnectionFactory.java:357)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory
> (BasicDataSource.java:2307)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionF
> actory(BasicDataSource.java:2290)
>
>
> It's
> >
> been a while since I've bothered to mock the JDBC interfaces
> because they are such a PITA to do. But it's possible that your mock
> Connection isn't being used by the BasicDataSource. Perhaps you need
> to mock the BasicDataSource (or maybe just DataSource) too.
>
> > so i think we should either override the validate() so the
> > validation is always returning true( which i am reluctant to do, as
> > the point is to test the whole thing)
> >
> > or enhancing the fake driver connection, so it always responding
> > with a result set.
>
> If you have set a validation query, that query should be executed. If
> you don't specify a query, I'd expect Connection.isValid to be used
> instead. I see from the exception that the isValid is failing, but you
> also mentioned that you tried to set a validation query and got a
> different error.
>
> I think you should keep going without a validation query because it
> should simplify things a little bit. Can you post more of your code?
> It's hard to tell how you are setting-up your test-case. If you have
> mock objects, what are you mocking and how are you injecting those
> mock objects into the code you are testing?
>
> - -chris
> -BEGIN PGP SIGNATURE-
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIcBAEBCAAGBQJXrKGFAAoJEBzwKT+lPKRYhfwP/AvTbY+aDWduuHChdoRAoPR2
> bjcA6u6QjCOxmpFT2qzMnp9PYNnUbqx6GpGyY0poSN6eX9lG5jc27r0SN1Hvrbwk
> Rp/+dFySnwMupnLccvhfZnqMZvoyVhPegdEwsbZ4Ep9zzE+wVK0hxAc/8V5nAZpY
> 5SLTYACt63i5pWyYTwxCHpVVYYyeSm+S9OH2nogD4QugDoV93g6K+YSLpM35SVHp
> u

Re: BasicDataSource creation throws SQLException

2016-08-11 Thread Roy Leonardus
Hello Christopher,

maybe i'm a little bit slow here, but from what i see, we should mock the
connection method

Connection connection= ds.getConnection();

something like

Connection connection =(Connection) EasyMock.expect(ds.
isvalid()).andReturn(true).anyTimes();

Problem is, the error happened during the validation of the
PoolableConnection, below is the build error from the surefire

Caused by: java.sql.SQLException: isValid() returned false
at
org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConnection.java:283)
at
org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:357)
at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2307)
at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2290)


so i think we should either override the validate() so the validation is
always returning true( which i am reluctant to do, as the point is to test
the whole thing)

or enhancing the fake driver connection, so it always responding with a
result set.

something like

private boolean isValid(int, int ) extend connection throws SQLException
{

  execute ("select 1));
 //another if else statment that we need / default result set
  return true;
}

Roy

On Wed, Aug 10, 2016 at 5:43 PM, Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> Roy,
>
> On 8/10/16 11:11 AM, Roy Leonardus wrote:
> > Hello Christopher,
> >
> > Thank you for the tip, i managed to test the code using the apache
> > Derby now.
> >
> > it would be interesting to enhance the driver class to respond with
> > some result set, but i need to review it again.
> >
> > and it will be another homework for me.
>
> Aren't you mocking-up the JDBC driver? Just add a return value for
> that call... something like:
>
> mockDriver.expect("next").return(true);
> mockDriver.expect("getInt", 1).return(0);
>
> You'll have to adapt that to your actual mocking framework, of course.
>
> - -chris
> -BEGIN PGP SIGNATURE-
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIcBAEBCAAGBQJXq0uOAAoJEBzwKT+lPKRY99gQAIw/khRNXErKjzoaqYFXjY4/
> GIdl8u2kH1V7AKePBK+o9EjggO+5qhHZOSFHsOGhiE0GbTIp3tRzRZ4/eBVit+cv
> dprp6eY1DtRznq1QahzGmzg+8XG7P7STbG1dE6Kj+0rnRAlWlmqCuLtT3WyOyolD
> FqfwedLJomQ+T6aFaY0MsdXHwfQ6m35EXHj5KrAOoFHWP7OLY/oXaFi2b1ztRRTB
> 9EY9Y8fuD08emRiv4cUyDiO1rz0mLRtkTS+WTMjg6OUogHkWu0fjXn0YFZx1shkh
> RrB7OyWTIc9EXOffp10enKv2LfoW71c+NbZY8GtiEswlkPrNIjR08xA5rzhzLm+q
> KpJFRea+kXwm1XI7M2o+r3MeqTg9WEghvOB3T7ZwnaCQ0weB5B5dTYnredpaYXEC
> 0PEmekF+69VuDbNYw6zAJVHtAbW5gSIEOl3mHKR2Em9PYKuleHpxpqABd5n/OBAG
> bZb/pWWamaVOQ8W5Joo+SHhRwH9TdrSDxgTk2e54FGUgjNu+XNy/0Qd1XLbOL4IY
> qaxzsnrHugHieUlw4dWeELr31wmX/BbQinIKK3XQQwKbTiK3fslP19NBpp0PbzY4
> SIBe+DxhKGygkyrj/FVYV+VrHtvc68zu2AH97RAOWlLrKB/KEqidCHdTGnRjQjUz
> cdXH+GaFUBo/zvtT4jhq
> =uDwn
> -END PGP SIGNATURE-
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: BasicDataSource creation throws SQLException

2016-08-10 Thread Roy Leonardus
Hello Christopher,

Thank you for the tip, i managed to test the code using the apache Derby
now.

it would be interesting to enhance the driver class to respond with some
result set, but i need to review it again.

and it will be another homework for me.

Roy

On Tue, Aug 9, 2016 at 8:54 PM, Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> Roy,
>
> On 8/3/16 10:03 AM, Roy Leonardus wrote:
> > i'm using the EasyMock to test the creation of the BasicDataSource
> > :
> >
> > BasicDataSource ds = prepareDataSource(); //set the properties of
> > the basic data source Connection connection = ds.getConnection();
> > // throw an error here
>
> Full stack trace please?
>
> > The properties that is set is
> >
> > bds.setMaxTotal(maxTotal); bds.setMinIdle(minIdle);
> > bds.setMaxIdle(maxIdle);
> > bds.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
> > bds.setMaxWaitMillis(maxWaitMillis);
> > bds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
> > bds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
> >
> >
> bds.setTestOnBorrow(true);
>
> Okay... most of those values aren't certain. Care to elaborate?
>
> > i do set the bds.setValidationQuery("Select 1") after checking the
> > source code and manage to proceed to have another error :
> >
> > *java.lang.NullPointerException: while trying to invoke the method
> > java.sql.PreparedStatement.executeQuery() of a null object loaded
> > from field
> > org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validationPreparedStat
> ement
> >
> >
> of an object loaded from local variable*
> >
> >> From what i see, the BDS expect a return row result from the
> >> driver, and as
> > i am using a "fake" driver, there is no return row, i'm just not
> > sure based on my trial.
> >
> > sorry if my question is a little bit weird, it is a first time for
> > me.
>
> A fake driver should be okay to use, but you do have to make sure it
> follows the rules... for example, ResultSets should return rows (or
> return false from a call to next) or throw an SQLException.
>
> - -chris
> -BEGIN PGP SIGNATURE-
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIcBAEBCAAGBQJXqibPAAoJEBzwKT+lPKRY8awP/3RFGqZnUgXwbSZmB6149gA5
> bPrJVqk3A1Y6V73DlaMA3jGWD09/G/QU6avNGcjfdHy01o37QmDAdTGwk1xOEIQn
> LBDy7GtzNVrj3x5wscg2FGq2M9oAh6FikNMS7ZyKHT6IrXOAjvGzM4C5V0pawBU/
> 0iJjKnEG6rW7e5PDZFFmteW4OVWnXQ25hrsUHzDJbLJ3e4cJSZGJvZFn0N5sGbiS
> 5tXhWPhegD8haVtvQcCamyzgII5UJtXMOQeR0Xzfr+U7YWj3o5cX2BDCVrclIAEi
> h9Zfrb6rqD45uvrkbHUbm1sm9R6aIILgk7TwPd7Hk3fldl2HyswMWAkdBnfs5pLr
> bZLEgpKi15e+RH71IILzmjMbbmkRyDSEtXWLUMOKFWMRK3sE0taTdRMuk7ktfwVU
> moUE945AUwBMWvZopsTSfnzpCReXG3uC4mSuIlLSLNyhtsbWrcC3rsNd5/h91h8p
> izS+MO2b0p+0auMMAscS/9Ch/fxxQAkBq/kk2S5eo/QpPxCZwhQTWJ7c6725YK1o
> lTWeGihiP1rb1cFYRE/A1vdKE5ZlO6MWc1FtH+nCwTZ3McJr9jNg/9plP60hHb/i
> aPrlND1BgW59C3OFnqxTVBaqQSlRVGxbjwVhDIggIAmjzemg1sxv3XJA+Je8mNKh
> NilChyWDRj5B6qXklWH9
> =mKAX
> -END PGP SIGNATURE-
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: BasicDataSource creation throws SQLException

2016-08-03 Thread Roy Leonardus
Hello Mark,

i'm using the EasyMock to test the creation of the BasicDataSource :

BasicDataSource ds = prepareDataSource(); //set the properties of the basic
data source
Connection connection = ds.getConnection(); // throw an error here

The properties that is set is


bds.setMaxTotal(maxTotal);
bds.setMinIdle(minIdle);
bds.setMaxIdle(maxIdle);
bds.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
bds.setMaxWaitMillis(maxWaitMillis);
bds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
bds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
bds.setTestOnBorrow(true);

i do set the bds.setValidationQuery("Select 1") after checking the source
code and manage to proceed to have another error :

*java.lang.NullPointerException: while trying to invoke the method
java.sql.PreparedStatement.executeQuery() of a null object loaded from
field
org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validationPreparedStatement
of an object loaded from local variable*

>From what i see, the BDS expect a return row result from the driver, and as
i am using a "fake" driver, there is no return row, i'm just not sure based
on my trial.

sorry if my question is a little bit weird, it is a first time for me.

Roy



On Wed, Aug 3, 2016 at 3:29 PM, Mark Thomas <ma...@apache.org> wrote:

> On 03/08/2016 01:46, Roy Leonardus wrote:
> > Dear All,
> >
> > I tried to create a new BasicDataSource and encountered such error
>
> Let me just see if I can find Pid's crystal ball so I know how you tried
> to create a new BasicDataSource and the state of the system on which you
> were trying to do that...
>
> > java.sql.SQLException: Cannot create PoolableConnectionFactory (isValid()
> > returned false)
> >
> > this is caused by this method in the PoolableConnection
>
> 
>
> > problem here is : i have not set any timeout with the
> > setValidationQueryTimeout, but the error appears.
> >
> >
> > I also do not encounter such error in my tomcat 7 runtime.
> >
> >
> > Could you guys give some hints? the expectation is of course to
> > eliminate such error
>
> With the information you have provided so far? No.
>
> You need to read this:
> http://www.catb.org/esr/faqs/smart-questions.html
>
> and then try again.
>
> Mark
>
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


BasicDataSource creation throws SQLException

2016-08-03 Thread Roy Leonardus
Dear All,

I tried to create a new BasicDataSource and encountered such error

java.sql.SQLException: Cannot create PoolableConnectionFactory (isValid()
returned false)

this is caused by this method in the PoolableConnection

  public void  
validate(String

sql, int timeout) throws SQLException

{

275 




if (_fastFailValidation

&& _fatalSqlExceptionThrown
)
{

276 




throw new SQLException
(Utils.getMessage
("poolableConnection.validate.fastFail"));

277 




}

278 




279 




if (sql == null || sql.length
()
== 0) {

280 




if (timeout < 0) {

281 




timeout = 0;

282 




}

283 




if (!isValid
(timeout))
{

284 




throw new SQLException
("isValid()
returned false");

285