java - Spring: run code before a persistence context is loaded -


i have spring based multimodule application. , in dao module db (embedded derby) started , created class implements applicationlistener.

problem in logs huge stacktrace spring there no db(couldn't connection).

still, application works without problems. stacktrace appeared before applicationlistener invoked , db created. actually, see when starting application first time on machine, because db created time, used.

so question whow avoid exception in logs? maybe there spring or hibenate setup not connect db before application context loaded? or invoke code creates db other listener?

well here way : root context contains datasource, dao, service , transaction manager. in xml config, declaration of database :

<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"       p:url="jdbc:derby:/path/to/database;create=true"        p:username="user" p:password="pwd"       p:driverclassname="org.apache.derby.jdbc.embeddeddriver"/> 

it can used declare session factory hibernate , associated dao :

<bean class="org.springframework.orm.hibernate4.localsessionfactorybean"       id="sessionfactory" p:datasource-ref="datasource">     <!-- hibernate config -->     ... </bean> <bean class="org.springframework.orm.hibernate4.hibernatetransactionmanager"       name="transactionmanager" p:sessionfactory-ref="sessionfactory"/> <tx:annotation-driven transaction-manager="transactionmanager"/> <bean id="mydao" class="... .mydaoimpl" p:sessionfactory-ref="sessionfactory" .../> 

that way created spring, ensures creation order correct. of course same possible in java config same logic.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -