-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Using existing DataSource
PostPosted: Mon Oct 19, 2009 5:05 am 
Newbie

Joined: Mon Oct 19, 2009 4:25 am
Posts: 1
Hi all,

I would like to integrate Hibernate with my existing code.
I've a service that build DataSources (no matter if from JNDI or from connection properties). I would like to use this service to supply Connections for my SessionManager but also implementing my ConnectionProvider I cannot assign a reference to my DataSource service because the ConnectionProvider is directly and implicitly created by Configuration.buildSessionFactory.

How could assign a reference to my service into my ConnectionProvider ?


Top
 Profile  
 
 Post subject: Re: Using existing DataSource
PostPosted: Tue Oct 27, 2009 10:49 am 
Newbie

Joined: Tue Oct 27, 2009 10:37 am
Posts: 6
Location: France
Hi,

You'll have to write your own ConnectionProvider, like below :

Code:
public class MyOwnDatasourceConnectionProvider implements ConnectionProvider
{

    private DataSource dataSource;

    public void configure(Properties arg0) throws HibernateException {
       
    }

    public Connection getConnection() throws SQLException {
        return this.getDataSource().getConnection();
    }

    public void closeConnection(Connection arg0) throws SQLException {
        if(this.getConnection() != null) this.getConnection().close();
    }

    public void close() throws HibernateException {
    }

    public boolean supportsAggressiveRelease() {
        return true;
    }

    public DataSource getDataSource()
    {
        if(this.dataSource == null)
        {
            this.dataSource = //... here your code that returns your own DataSource instance
        }
        return this.dataSource;
    }
}


and define your ConnectionProvider in Hibernate's configuration. For example, in HibernateUtil :

Code:
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
   

    static {
        try {           
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration().configure();

            // Define my own ConnectionProvider (watch out connection parameters from hibernate.cfg.xml will no longer be used)
            configuration.setProperty(Environment.CONNECTION_PROVIDER, MyOwnDatasourceConnectionProvider.class.getName());

            sessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

_________________
http://jnesis.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.