Spring, BasicDataSource and OpenJPA
So you want to use Spring with commons BasicDataSource and OpenJPA? There is an interesting gotcha.
Let’s start with just the DriverManager DataSource for comparison. In your spring context you define the datasource like this:
1 2 3 4 |
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:hsql://localhost:9001/testdb" p:username="sa" p:password="" /> |
Then your EntityManager Factory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceUnitName" value="jpamaven-test" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" /> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="database" value="HSQL" /> <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.HSQLDictionary" /> </bean> </property> <property name="jpaPropertyMap"> <map> <entry key="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver" /> <entry key="openjpa.ConnectionURL" value="jdbc:hsqldb:hsql://localhost:9001/testdb" /> <entry key="openjpa.ConnectionUserName" value="sa" /> <entry key="openjpa.ConnectionPassword" value="" /> <entry key="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add', ForeignKeys=true)" /> <entry key="openjpa.jdbc.DBDictionary" value="hsql" /> <entry key="openjpa.jdbc.Schema" value="PUBLIC" /> <entry key="openjpa.Log" value="DefaultLevel=TRACE, Runtime=TRACE, Tool=TRACE, SQL=TRACE" /> <entry key="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72" /> </map> </property> <property name="jpaDialect" ref="jpaDialect" /> </bean> |
Here META-INF/persistence.xml is almost empty because all the properties are specified here in Spring. See the Spring docs for more info.
The trouble starts when you change your datasource to this:
1 2 3 4 5 6 |
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:hsql://localhost:9001/testdb" p:username="sa" p:password="" /> |
It “should” just work. No such luck. To make a long story short you have to change the openjpa properties.
Here is the combination that works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<property name="jpaPropertyMap"> <map> <entry key="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource" /> <entry key="openjpa.ConnectionProperties" value="DriverClassName=org.hsqldb.jdbcDriver,Url=jdbc:hsqldb:hsql://localhost:9001/testdb,Username=sa,Password=" /> <entry key="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add', ForeignKeys=true)" /> <entry key="openjpa.jdbc.DBDictionary" value="hsql" /> <entry key="openjpa.jdbc.Schema" value="PUBLIC" /> <entry key="openjpa.Log" value="DefaultLevel=TRACE, Runtime=TRACE, Tool=TRACE, SQL=TRACE" /> <entry key="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72" /> </map> </property> |
You have to gang up four properties into openjpa.ConnectionProperties. Intuitive huh?
One Reply to “Spring, BasicDataSource and OpenJPA”