Is it possible to configure Hibernate to use a javax.sql.DataSource instance?
My application already has an instance of javax.sql.DataSource and I’d rather not re-configure the database url, user, password, driver etc just for hibernate.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the data source is available through JNDI, you just need to set the hibernate.connection.datasource property of the configuration – mtpettyp’s answer demonstrates that.
If you’re not in an environment where your data sources come from JNDI, provide your own ConnectionProvider implementation, and either pass it to the Settings object before building the session factory, or specify its class name in the hibernate.connection.provider_class property. If you provide an instance to Settings, you can probably instantiate the standard DatasourceConnectionProvider and give it your DataSource.