How to retrieve DB connection using DataSource without JNDI? - java

We want our own db connection configuration instead of using JNDI, but in the meantime, we also want to use DataSource instead of using DriverManager, how to do so?

You use a connection pool library like c3p0 or commons dbcp.
C3P0
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("dbuser");
cpds.setPassword("dbpassword");
Connection connection = cpds.getConnection();
DBCP
BasicDataSource ds= new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl("jdbc:postgresql://localhost/testdb");
ds.setUsername("dbuser");
ds.setPassword("dbpassword");
Connection connection = ds.getConnection();

You can use org.apache.commons.dbcp.BasicDataSource
BasicDataSource ds= new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:#dburl:port:sid");
ds.setUsername("uname");
ds.setPassword("pass");

Related

Set jdbc interceptor using DriverManager class

I am using DriverManager.getConnection(url, prop) to get the connection. I am trying to inject the jdbc interceptors using properties like below but it is not working.
Properties prop = new Properties();
...
prop.setProperty("jdbcInterceptors", "com.amazonaws.xray.sql.mysql.TracingInterceptor;");
However, when we try to do via datasource it is working.
import org.apache.tomcat.jdbc.pool.DataSource;
DataSource source = new DataSource();
source.setUrl("url");
source.setUsername("user");
source.setPassword("password");
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setJdbcInterceptors("com.amazonaws.xray.sql.mysql.TracingInterceptor;");
Not sure what is wrong with DriverManager properties.
These interceptors are a feature of the Tomcat org.apache.tomcat.jdbc.pool.DataSourceProxy and its subclass org.apache.tomcat.jdbc.pool.DataSource. This is not a feature of JDBC itself, nor a feature of the JDBC driver you're using, so the only way to access it is through a Tomcat data source.
In short, it doesn't work with DriverManager because this feature doesn't exist in DriverManager.

Create SqlSession from existing Connection directly

I'm working with existing Java code wherein there is an existing JDBC connection pooling mechanism on deployed systems and an already existing setup to get JDBC connections. I'd like to leverage this to create a MyBatis SqlSession object without creating a Configuration, DataSource, and other things
I have code that already creates a java.sql.Connection object is given the desired resource. I'd like to leverage this and get that SqlSession object and use MyBatis from that point onwards.
I don't want MyBatis to manage connection pooling, determining which data source to use, etc. Is this possible?
I'm afraid you can't avoid creation of the Configuration object. It is used by the internal mybatis machinery like executors. But even if you could it will not help you much. In this case you will need to implement most of Configuration functionality yourself so it does not make sense to do that.
You main goal is to be able to use SqlSessionFactory.openSession(Connection connection) method. To do this you need to have SqlSessionFactory. The easiest way for you is to create Configuration like it is descried in mybatis documentation:
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
// set properties to configuration here
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);
Now if your connection pool does implement DataSource you use it directly to create environment. If it does not you need to create an adapter around your pool which implements javax.sql.DataSource.
My solution is similar to Roman's above, but I needed to create an Oracle datasource. For various reasons, the connection needs to be created using the Class.forName type sequence
Class.forName("oracle.jdbc.driver.OracleDriver");
String connectionString = "jdbc:oracle:thin:#//yadayada";
String username = "myusername";
String password = "mypassword";
OracleDataSource oracleDataSource = new OracleDataSource();
oracleDataSource.setURL(connectionString);
oracleDataSource.setPassword(password);
oracleDataSource.setUser(username);
environment = new Environment("dev",transactionFactory,oracleDataSource);
configuration = new Configuration(environment);
configuration.addMappers("MyMybatisMapper");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
return sqlSessionFactory.openSession();
What I was missing was the OracleDataSource object.

Spring JPA Hikari multiple connection pool with same datasource

I'm using Spring JPA for connecting to the data source. My requirement is to have multiple connection pools to the same data source so that I can manage the DB operations based on priority
Is there a way to have multiple connection pools with the same data source?
I was going through this example and I want to do almost the same thing but with the same datasource using Spring JPA
Yes, you can create even the same DataSource just with different pool name.
For example method the will create DataSource with different pool name:
private javax.sql.DataSource dataSource(String poolName) {
final HikariDataSource dataSource = new HikariDataSource();
//...setup DataSource properties
dataSource.setPoolName(poolName);
}

List connections in use of a JDBC Data Source

How do i list all connections that are in use?
This is my code:
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("jdbc/xxxxxx");
con = dataSource.getConnection();
So, having this, i want to list all connections that are currently been used to monitor the system and if needed, close the connections.
You can create a util wrapper where getConnection() will increment and returnConection will decrement. You can use connection pool like dbcp which has BasicDataSource has method getNumIdle() and getNumActive() for this kind of matrics

Tomcat6 connect to mySQL problems

I've followed another stackover flow thread to get to this point, it was this one here:
Tomcat6 MySql JDBC Datasource configuration
The problem I have is that the line that goes:
Connection conn = ds.getConnection();
from this block:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Eclipse gives me the error getConnection() is undefined for the type DataSource.
Its solution is to do this:
Connection conn = ((java.sql.Statement) ds).getConnection();
No tutorials show the need to do this, and its not working when I do that. I'm using mySQL jar named, mysql-connector-java-5.1.18-bin I've used it with RMI before but never Tomcat, is it the correct type for use with Tomcat?
TIA
If I look into the Java API docs http://docs.oracle.com/javase/6/docs/api/ I find the javax.sql.DataSource interface with a getConnection() method. I assume your DataSource to be something else than one implementing the javax.sql.DataSource interface. What "DataSource" is imported?

Categories