Get java sql connection from org hibernate Session
Hi we can get java sql connection object from org hibernate session object.
As i write a function getJavaSqlConnectionFromHibernateSession, which take session as an argument and return connection object.
private Connection getJavaSqlConnectionFromHibernateSession(Session session){
SessionFactoryImplementor sessionFactoryImplementor = null;
ConnectionProvider connectionProvider = null;
java.sql.Connection connection = null ;
try {
sessionFactoryImplementor = (SessionFactoryImplementor)session.getSessionFactory();
connectionProvider = (ConnectionProvider)sessionFactoryImplementor.getConnectionProvider().getConnection();
connection = connectionProvider.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
----------------------------------------------------------
Even we can use such kind of stuff.
private void doDBWork(){
session.doWork(
new Work() {
public void execute(Connection connection) throws SQLException
{
// here you get the java sql connection object and do any DB Work with it
}
}
);
session.disconnect();
}
Above stuff is suggested by JBoss. For more info you can visit Hibernate Session API docs at following url
http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html
Hi we can get java sql connection object from org hibernate session object.
As i write a function getJavaSqlConnectionFromHibernateSession, which take session as an argument and return connection object.
private Connection getJavaSqlConnectionFromHibernateSession(Session session){
SessionFactoryImplementor sessionFactoryImplementor = null;
ConnectionProvider connectionProvider = null;
java.sql.Connection connection = null ;
try {
sessionFactoryImplementor = (SessionFactoryImplementor)session.getSessionFactory();
connectionProvider = (ConnectionProvider)sessionFactoryImplementor.getConnectionProvider().getConnection();
connection = connectionProvider.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
----------------------------------------------------------
Even we can use such kind of stuff.
private void doDBWork(){
session.doWork(
new Work() {
public void execute(Connection connection) throws SQLException
{
// here you get the java sql connection object and do any DB Work with it
}
}
);
session.disconnect();
}
Above stuff is suggested by JBoss. For more info you can visit Hibernate Session API docs at following url
http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html
Good article!!!
ReplyDelete