spring - JDBC URL is null? -
depending on spring profile hibernate should update (development) or validate (production) db schema. i'm getting exception:
java.sql.sqlexception: url cannot null @ java.sql.drivermanager.getconnection(drivermanager.java:556) @ java.sql.drivermanager.getconnection(drivermanager.java:187) @ org.springframework.jdbc.datasource.drivermanagerdatasource.getconnectionfromdrivermanager(drivermanagerdatasource.java:173) @ org.springframework.jdbc.datasource.drivermanagerdatasource.getconnectionfromdriver(drivermanagerdatasource.java:164) @ org.springframework.jdbc.datasource.abstractdriverbaseddatasource.getconnectionfromdriver(abstractdriverbaseddatasource.java:153) @ org.springframework.jdbc.datasource.abstractdriverbaseddatasource.getconnection(abstractdriverbaseddatasource.java:119) @ org.hibernate.service.jdbc.connections.internal.datasourceconnectionproviderimpl.getconnection(datasourceconnectionproviderimpl.java:141) @ org.hibernate.tool.hbm2ddl.suppliedconnectionproviderconnectionhelper.prepare(suppliedconnectionproviderconnectionhelper.java:51) @ org.hibernate.tool.hbm2ddl.schemaupdate.execute(schemaupdate.java:194) @ org.hibernate.tool.hbm2ddl.schemaupdate.execute(schemaupdate.java:178) @ org.hibernate.internal.sessionfactoryimpl.<init>(sessionfactoryimpl.java:505) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1769) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1840) @ org.springframework.orm.hibernate4.localsessionfactorybuilder.buildsessionfactory(localsessionfactorybuilder.java:247) @ com.example.config.mainconfig.sessionfactory(mainconfig.java:42) @ com.example.config.mainconfig$$enhancerbycglib$$3cbd4ba6.cglib$sessionfactory$2(<generated>) @ com.example.config.mainconfig$$enhancerbycglib$$3cbd4ba6$$fastclassbycglib$$524bdff7.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invokesuper(methodproxy.java:228) @ org.springframework.context.annotation.configurationclassenhancer$beanmethodinterceptor.intercept(configurationclassenhancer.java:286) @ com.example.config.mainconfig$$enhancerbycglib$$3cbd4ba6.sessionfactory(<generated>) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:160) @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:570) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiateusingfactorymethod(abstractautowirecapablebeanfactory.java:1025) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:921) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:487) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:458) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:295) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:223) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:292) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:626) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:932) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:479) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:389) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:294) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:112) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4797) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5291) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1559) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1549) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:334) @ java.util.concurrent.futuretask.run(futuretask.java:166) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:724)
this configuration:
@configuration @profile("development") public class developmentconfig { public @bean string profile() { return "development"; } } @configuration @componentscan(basepackages = "com.example", excludefilters = { @componentscan.filter(configuration.class) }) @propertysource("classpath:application.properties") @enabletransactionmanagement public class mainconfig { private @autowired environment env; private @autowired string profile; public @bean datasource datasource() throws ioexception { drivermanagerdatasource ret = new drivermanagerdatasource(); ret.setconnectionproperties(propertyutils.getproperties(profile, "jdbc")); return ret; } @suppresswarnings("deprecation") // http://stackoverflow.com/questions/14806400/configure-sessionfactory-with-spring-hibernate-and-localsessionfactorybuilder public @bean sessionfactory sessionfactory() throws ioexception { return new localsessionfactorybuilder(datasource()) .scanpackages("com.example.entity") .addproperties(propertyutils.getproperties(profile, "hibernate")) .buildsessionfactory(); } public @bean hibernatetransactionmanager transactionmanager() throws ioexception { return new hibernatetransactionmanager(sessionfactory()); } } public class propertyutils { private static final logger log = loggerfactory.getlogger(propertyutils.class); public static properties getproperties(string profile, string filename) throws ioexception { log.debug("read " + filename + ".properties profile " + profile); properties ret = new properties(); classpathresource resource; resource = new classpathresource("properties/common/" + filename + ".properties"); ret.putall(propertiesloaderutils.loadproperties(resource)); resource = new classpathresource("properties/" + profile + "/" + filename + ".properties"); if (resource.exists()) { ret.putall(propertiesloaderutils.loadproperties(resource)); } log.debug(ret.tostring()); return ret; } }
first class indicates profile use (there's similar productionconfig
well). second class creates necessary stuff (datasource, sessionfactory , transactionmanager). third 1 merges common properties , profile dependant properties.
based on development profile properties (merged):
hibernate.properties:
hibernate.dialect=org.hibernate.dialect.postgresqldialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update
jdbc.properties:
driverclassname=org.postgresql.driver url=jdbc:postgresql://localhost:5432/mydb username=******** password=********
however, instead of updating db schema i'm getting exception. never debug log output read jdbc.properties profile development
. so, never read... why?
got it. thought org.springframework.jdbc.datasource.abstractdriverbaseddatasource.setconnectionproperties
shortcut setdriverclassname
, setusername
, setpassword
, seturl
. isn't , need set explicitly:
public @bean datasource datasource() throws ioexception { properties jdbcproperties = propertyutils.getproperties(profile, "jdbc"); drivermanagerdatasource ret = new drivermanagerdatasource(); ret.setdriverclassname(jdbcproperties.getproperty("driverclassname")); ret.setusername(jdbcproperties.getproperty("username")); ret.setpassword(jdbcproperties.getproperty("password")); ret.seturl(jdbcproperties.getproperty("url")); return ret; }
Comments
Post a Comment