Archive

Posts Tagged ‘persistence.xml’

java.lang.UnsupportedOperationException: The user must supply a JDBC connection

26 November 2010 5 comments

If you get this with Hibernate it probably means that you have an error, or an incomplete persistence.xml file. I got this while migrating from JBoss to WebLogic server and had forgotten to migrate the configuration from JBoss’ *-ds.xml to persistence.xml.

It happens because of this bit of code (Hibernate’s ConnectionProviderFactory.java)

ConnectionProvider connections;
String providerClass = properties.getProperty(Environment.CONNECTION_PROVIDER);
if ( providerClass!=null ) {
	try {
		log.info("Initializing connection provider: " + providerClass);
		connections = (ConnectionProvider) ReflectHelper.classForName(providerClass).newInstance();
	}
	catch ( Exception e ) {
		log.error( "Could not instantiate connection provider", e );
		throw new HibernateException("Could not instantiate connection provider: " + providerClass);
	}
}
else if ( properties.getProperty(Environment.DATASOURCE)!=null ) {
	connections = new DatasourceConnectionProvider();
}
else if ( properties.getProperty(Environment.URL)!=null ) {
	connections = new DriverManagerConnectionProvider();
}
else {
	connections = new UserSuppliedConnectionProvider();
}

It tries to load several connection providers based on the Environment properties, it can’t find any connection providers (since their corresponding properties are not defined in the persistence.xml) and it just returns the default UserSuppliedConnectionProvider which, alas, it’s getConnection method is the following

public Connection getConnection() {
	throw new UnsupportedOperationException("The user must supply a JDBC connection");
}

After fixing the error, my persistence.xml is the following

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
             version="1.0">
             
   <persistence-unit name="myUnit" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <jta-data-source>myDatasource</jta-data-source>

      <properties>
		<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DatasourceConnectionProvider"/>
	   	<property name="hibernate.connection.datasource" value="myDatasource"/>
	   	<property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
	   	<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
	   	<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.default_schema" value="MYSCHEMA"/>
	 <property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/>
      </properties>
   </persistence-unit>  
    
</persistence>