I'm trying to deploying a java app to Heroku.
It use a enviroment variable such as postgres://foo:foo#heroku.com/hellodb to config database in the spring applicationcontext.xml https://devcenter.heroku.com/articles/spring-mvc-hibernate#modify_database_configuration
I follow the document,config it,and set the DATABASE_URL use set DATABASE_URL=postgres://postgres:huang#heroku.com/yan(windows).
But when i run it locally,i got a Error:
creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/applicationContext.xml]: Initialization of bean failed; nested exception is o
rg.springframework.beans.factory.BeanExpressionException: Expression parsing fai
led; nested exception is java.lang.IllegalStateException: Cannot handle (64) '#'
my spring xml is this:
`<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemEnvironment['DATABASE_URL']}" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url"
value="#{ 'jdbc:postgresql://' + #dbUrl.getHost() + #dbUrl.getPath() }" />
<property name="username" value="#{ #dbUrl.getUserInfo().split(':')[0] }" />
<property name="password" value="#{ #dbUrl.getUserInfo().split(':')[1] }" />
</bean>`
it seems the spring container cannot handle the '#' symbol ,but i really do not know how to deal it! I searched in google,but i got anything.
Does anyone know how to deal it ? thanks!
yeah,i fix it!!!
the official document has a error!!!
i just remove the "#" symbol,and it works
this spring xml is like this:
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemEnvironment['DATABASE_URL']}" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url"
value="#{ 'jdbc:postgresql://' + dbUrl.getHost() + dbUrl.getPath() }" />
<property name="username" value="#{ dbUrl.getUserInfo().split(':')[0] }" />
<property name="password" value="#{ dbUrl.getUserInfo().split(':')[1] }" />
</bean>
i will write a letter to report this error
Related
(I hope this below makes sense, I had a bit of a struggle with formatting).
We have an application (currently being refreshed from technology of circa 2005) whose owners require the use of Oracle AQ for message handling.
We are using the Spring JDBC extensions, and when the application begins to listen to the queue it gets a ClassCastException saying "X cannot be cast to oracle.jdbc.internal.OracleConnection".
Where X is either a WrappedConnectionJDK7 from jboss when we use a datasource defined in Wildfly e.g.
<bean id="dataSource-supsrep"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:XE" />
<property name="username" value="...." />
<property name="password" value="...." />
</bean>
or X is a PoolGuardConnectionWrapper from commons.dbcp when we define the datasource like this:
<bean id="dataSource-supsrep"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:XE" />
<property name="username" value="...." />
<property name="password" value="...." />
</bean>
Message Listener bean is defined like this :
<bean id="messageListener" class="xxx.report.dispatch.DispatchMDB" />
Message Listener container bean is like this :
<bean id="reportProcessorXslfoHigh"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="sessionTransacted" value="true" />
<property name="connectionFactory" ref="AQconnectionFactory" />
<property name="destinationName" value="Q1_XSLFO_HIGH" />
<property name="messageListener" ref="messageListener" />
<property name="transactionManager" ref="transactionManager" />
<property name="concurrentConsumers" value="1" />
<property name="maxConcurrentConsumers" value="1" />
<property name="receiveTimeout" value="30000" />
<property name="idleTaskExecutionLimit" value="30" />
<property name="idleConsumerLimit" value="1" />
</bean>
The connection factory is like this :
<orcl:aq-jms-connection-factory id="AQconnectionFactory"
data-source="dataSource-supsrep" native-jdbc- extractor="oracleNativeJdbcExtractor" />
and it uses a Native JDBC Extractor :
<bean id="oracleNativeJdbcExtractor"
class="org.springframework.jdbc.support
.nativejdbc.OracleJdbc4NativeJdbcExtractor" />
So my question is - can this be overcome by Spring configuration details - or is there another way round it? Clearly the underlying connection is needed but after 3 days of research I cannot see how it can be done.
Any clues gratefully received !
Thanks,
John
I am trying to configure 2 different datasources in my application as it is required.
My configuration in the AppContext.xml is as follows:
<bean id="dataSourceA" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="datasourceAURL"/>
<property name="username" value="aaaa"/>
<property name="password" value="pppp"/>
</bean>
<bean id="dataSourceB" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="datasourceBURL"/>
<property name="username" value="bbbb"/>
<property name="password" value="cccc"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com/ex/myBatis/mappings" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceA" />
<property name="typeAliasesPackage" value="com.ex.myBatis.entities"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="configDataSource" />
<property name="typeAliasesPackage" value="com.ex.myBatis.entities"/>
</bean>
<bean id="sqlSession1" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory1" />
</bean>
<bean id="callService" class="com.ex.myBatis.Service.callService">
<property name="sqlSession" ref="sqlSession1" />
</bean>
But while accessing the bean I am getting the below Exception
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'callService': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire method: public final void
org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate);
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [org.mybatis.spring.SqlSessionTemplate] is
defined: expected single matching bean but found 2:
sqlSession,sqlSession1
Please somebody help me in figuring out the issue.
Also please suggest me if there is someother way to configure 2 Datasources.
Your configuration is mostly correct. The problem you face is that you use autowiring to inject one of callService dependencies.
Seems that you use SqlSessionDaoSupport and its sqlSessionTemplate field is autowired. There are two templates defined so spring cannot automatically wire dependencies. You need specify correct template manually.
my spring XML is below,
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
I'm creating the spring bean context when the server is starting up. When I hit on the submit button of the JSP page, it should call the servlet and executes the SQL Query.
Without doing JdbcTemplate jTemplate = (JdbcTemplate)context.getBean("jTemplate") is there anyway I can get the jTemplate object automatically injected to my java property?
my java property is this,
private JdbcTemplate jTemplate;
So, simply I want to use the jTemplate without just doing JdbcTemplate jTemplate = (JdbcTemplate)context.getBean("jTemplate")
Sorry guys I'm bit new to Spring, If you don't get what I'm saying please ask me again.
Use autowiring which can be in three ways
By name
By Type
By Constructor
Further reading is available here
like one solution is
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" autowire="byName">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean name="jTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
Use #Autowired annotation to get this bean automatically like this.
#Autowired
private JdbcTemplate jTemplate;
We are working on a maven-web project.In this project two databases used, MySQL and Neo4j.
Project is running fine with MySQL(without Neo4j). After adding Neo4j config file(neo4j-config.xml) project build without any error, insert operation to Mysql return successfully but actually nothing inserted to the tables.
With some research about this problem found information about "cross store operation":
XML configuration with cross-store
<context:annotation-config/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>
<datagraph:config graphDatabaseService="graphDatabaseService"
entityManagerFactory="entityManagerFactory"/>
<bean id="graphDatabaseService"
class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="http://localhost:7474/db/data" />
</bean>
Our project db-context.xml
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="database" value="MYSQL" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
Our Project neo4j-context.xml
<neo4j:config graphDatabaseService="graphDatabaseService" />
<bean id="graphDatabaseService"
class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="http://localhost:7474/db/data" />
</bean>
After adding entityManagerFactory="entityManagerFactory" to our neo4j-config.xml file
we got an error like below while running maven project:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating b ean with name
'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Error se
tting property values; nested exception is
org.springframework.beans.NotWritableProp ertyException: Invalid
property 'entityManagerFactory' of bean class [org.springfram
ework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$5a2edf0]:
Bean property 'entityManagerFactory' is not writable or has an
invalid setter method. Does the pa rameter type of the setter match
the return type of the getter?
if we use datagraph tag instead of neo4j tag, i got an error like below
Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]; nested ex ception is
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Li ne 32 in XML document from ServletContext resource
[/WEB-INF/neo4j-config.xml] is in valid; nested exception is
org.xml.sax.SAXParseException; systemId:
http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd;
lineNumber: 32; columnNumber: 51 ; src-resolve: Cannot resolve the
name 'repository:repository' to a(n) 'type definit ion' component.
Our problem is that although insert operation line returns success in debug mode, it does not add new record to table after adding neo4j config file to our project. We did not understand why this situation occurs with only adding neo4j-config.xml file. Second confusion is why adding "entityManagerFactory" does not solve this problem.
We couldn't find any example of Neo4j working with MySQL in maven-web project except spring-data example (myrestaurants-social) .
Thank you for all valuable responses in advance.
hi i am using spring config.xml to config my embedded database hsqldb. my spring config is as below:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql" />
</jdbc:embedded-database>
<bean id="adapterDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="datasource" ref="dataSource"></property>
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:data/db/lmexadapter_db" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
when run run my appication which is using above spring config.xml it give me a exception as below:
org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'datasource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
please help to resolve this as soon as possible
Thank you
The issue is that org.apache.commons.dbcp.BasicDataSource doesn't have a "setDatasource" method. It does have a protected field "datasource", however, which you could expose by subclassing and providing a setter.