I was trying to follow the notes from
this forum post
but I couldn’t get it to work.
I’ve only just learned about connection pools so I have very little knowledge in setting it up, but I’m trying. Please don’t get too mad.
We have a MS SQL server like this:
I want to access table1 in DB2
According to a tutorial, I’m suppose to go to Eclipse’s
Server > Server.xml
and add the following code.
<Resource name="jdbc/ServerName"
auth="Container"
factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
type="javax.sql.DataSource"
maxActive="50"
maxIdle="10"
maxWait="15000"
username="username"
password="password"
url="jdbc:sqlserver://ServerName;instance=SQLEXPRESS;databaseName=DB2;"
removeAbandoned="true"
removeAbandonedTimeout="30"
logAbandoned="true" />
However, I read
somewhere else
it’s actually suppose to be in Context.xml? Which I can’t find in my Vaadin webapp. in
This tutorial,
it says it should be under the meta-inf folder but Every time I create a context.xml file there, it gets eaten when I compile the program.
Next I add the drivers to my Tomcat library? I downloaded the microsoft ones from
here
And I add this code to my java program:
[code]
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context ctx = (Context) new InitialContext().lookup(“java:comp/env”);
BasicDataSource ds = (BasicDataSource)ctx.lookup(“jdbc/ServerName”);
con = ds.getConnection();
String sql = "select * from DB2.dbo.Table1 where [Field1]
IS NULL";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
//Add in all info
while (rs.next()){
beanResultsList.addBean(new Bean(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDate(4),
rs.getDate(5),
rs.getInt(6),
rs.getInt(7),
rs.getInt(8),
rs.getInt(9),
rs.getString(10),
rs.getString(11),
rs.getString(12),
rs.getString(13)
));
}
} catch (SQLException | NamingException e) {
e.printStackTrace();
}finally{
try { con.close(); } catch (SQLException e) {}
try { rs.close(); } catch (SQLException | NullPointerException e) {}
try { stmt.close(); } catch (SQLException e) {}
} // End finally (try catch)
[/code]And it doesn’t work, I get nullpointerexception on the Con.close() because it’s not reaching the sql server?
However, just using JTDS works fine, but it’s really slow.
[code]
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
con = DriverManager.getConnection(“jdbc:jtds:sqlserver://ServerName;instance=SQLEXPRESS”,“username”,“password”);
String sql = "select * from DB2.dbo.Table1 where [Field1]
IS NULL";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
//Add in all info
while (rs.next()){
beanResultsList.addBean(new Bean(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDate(4),
rs.getDate(5),
rs.getInt(6),
rs.getInt(7),
rs.getInt(8),
rs.getInt(9),
rs.getString(10),
rs.getString(11),
rs.getString(12),
rs.getString(13)
));
}
} catch (SQLException | NamingException e) {
e.printStackTrace();
}finally{
try { con.close(); } catch (SQLException e) {}
try { rs.close(); } catch (SQLException | NullPointerException e) {}
try { stmt.close(); } catch (SQLException e) {}
} // End finally (try catch)
[/code]Does anybody who has gotten connection pooling to work know what’s wrong? And how do I get Context.xml in my vaadin app? And keep it there?