I use Spring-configured jms template with tibco jms library.
I get jms connection factory and topic with JNDI and these objects are not null. But when I try to send message or add listener I get this exception:
For listener:
Exception in thread "main" org.springframework.jms.InvalidDestinationException: Can not send into foreign destinations; nested exception is javax.jms.InvalidDestinationException: Can not send into foreign destinations
at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:277)
at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:474)
at org.springframework.jms.core.JmsTemplate.receiveSelected(JmsTemplate.java:700)
at org.springframework.jms.core.JmsTemplate.receive(JmsTemplate.java:682)
at org.springframework.jms.core.JmsTemplate.receive(JmsTemplate.java:674)
For sender:
Exception in thread "main" org.springframework.jms.InvalidDestinationException: Invalid or foreigndestination; nested exception is javax.jms.InvalidDestinationException: Invalid or foreigndestination
at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:277)
at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:474)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:539)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:531)
Client app is working with the same topic without problems (so jms server is running). Do you have any ideas? I read about this exception in javadoc, but can't find how to understand the root issue and fix it.
Thanks
UPD:
JMS-related part of config:
<bean id="JmsFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jms.factory}"/>
<property name="proxyInterface" value="javax.jms.TopicConnectionFactory" />
<property name="lookupOnStartup" value="false" />
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">${jms.namingProvider}</prop>
<prop key="java.naming.factory.initial">${jms.namingFactory}</prop>
<prop key="java.naming.referral">${jms.namingReferral}</prop>
<prop key="java.naming.security.credentials">${jms.securityCredentials}</prop>
<prop key="java.naming.security.principal">${jms.securityPrincipal}</prop>
</props>
</property>
</bean>
<bean id="JmsTopic" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jms.topic}"/>
<property name="proxyInterface" value="javax.jms.Topic" />
<property name="lookupOnStartup" value="false" />
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">${jms.namingProvider}</prop>
<prop key="java.naming.factory.initial">${jms.namingFactory}</prop>
<prop key="java.naming.referral">${jms.namingReferral}</prop>
<prop key="java.naming.security.credentials">${jms.securityCredentials}</prop>
<prop key="java.naming.security.principal">${jms.securityPrincipal}</prop>
</props>
</property>
</bean>
<bean id="UserCredentialsConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory">
<ref bean="JmsFactory" />
</property>
<property name="username" value="${jms.user}" />
<property name="password" value="${jms.password}" />
</bean>
<bean id="JmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory"
ref="UserCredentialsConnectionFactory" />
<property name="defaultDestination">
<ref bean="JmsTopic"/>
</property>
<property name="pubSubDomain" value="true" />
</bean>
It sounds like you have it configured so that it is attempting to create a destination rather than do a jndi lookup to get the destination that has been defined on the EMS instance.
You need to post your spring config to be sure though.
edit: if you set a destinationname on the JmsTemplate and provide it with a JndiDestinationResolver then it should work
Related
We have an application where we have implemented Abstract Routing Data source feature in dealing multiple data bases of same type mysql (jdbc:mysql://127.0.0.1:3306/test, jdbc:mysql://127.0.0.1:3306/test2).
Now we are going with different database which is DB2. so i was unable to find any samples for this scenario using abstract routing datasource.
Can anyone give any directions ?
This is my code block:
i have defined 3 datasources itemDataSource, custDataSource, orderDb2DataSource in dao.xml .
how to configure the second session factory to the the transaction manager ?
<bean id="dataSource" class="com.test.DatabaseRoutingDataSource">
<property name="targetDataSources">
<map key-type="com.test.dao.DatabaseType">
<entry key="ITEM" value-ref="itemDataSource"/>
<entry key="CUSTOMER" value-ref="custDataSource"/>
<entry key="ORDER_DB2" value-ref="orderDb2DataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="itemDataSource" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySqlDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Added for DB2 Database -->
<bean id="sessionFactoryDB2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>/WEB-INF/hibernate.db2.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Added for DB2 Database -->
<bean id="transactionManager1"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
I am setting up a Spring hibernate H2 application. When the server starts everything works but after an update (rebuild project in eclipse without restarting Tomcat) I get the following error messages saying that the database file can't be accessed.
Error Messages:
java.lang.IllegalStateException: The file is locked: nio:/home/bob/dataStore.mv.db [1.4.187/7]
java.nio.channels.OverlappingFileLockException
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Database may be already in use:
After Googling this error I tried adding File_LOCK=NO and DB_CLOSE_ON_EXIT=TRUE to the URL but no luck.
Context.xml file
<context:annotation-config />
<context:component-scan base-package="com" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<!-- <property name="url" value="jdbc:h2:tcp://localhost/~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE" /> -->
<property name="url" value="jdbc:h2:file:~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE;MVCC=TRUE" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
What can I do so that the database does not get locked every time the project is rebuilt.
Also, eclipse is rebuilding the application after everytime the database is updated. How can I stop this?
Try DB_CLOSE_ON_EXIT=FALSE, from the Spring docs
If, for whatever reason, you do configure the connection URL for an embedded database, care should be taken to ensure that the database’s automatic shutdown is disabled. If you’re using H2 you should use DB_CLOSE_ON_EXIT=FALSE to do so.
if need to create multipul connecation then you need to use below code :
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:\Database\Data\production;"/>
I've catched java.sql.SQLTransientConnectionException: springHikariCP - Connection is not available, request timed out after 30001ms.
First code block works well, second (CP) does not work.
What is wrong and how fix this?
JDK - 1.8.0_73.
HikariCP - 2.4.5.
Spring - 4.2.5.RELEASE.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${connection.driverClassName}"/>
<property name="username" value="${connection.userName}"/>
<property name="password" value="${connection.password}"/>
<property name="url" value="${connection.url}"/>
</bean>
CP
<bean id="hikariConfiguration" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP"/>
<property name="dataSourceClassName" value="${connection.dataSourceClassName}"/>
<property name="maximumPoolSize" value="${connection.pool.maximumPoolSize}"/>
<property name="idleTimeout" value="${connection.pool.idleTimeout}"/>
<property name="dataSourceProperties">
<props>
<prop key="url">${connection.url}</prop>
<prop key="user">${connection.userName}</prop>
<prop key="password">${connection.password}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy- method="close">
<constructor-arg ref="hikariConfiguration"/>
</bean>
//...............
#CONNECTION
connection.dataSourceClassName=org.hsqldb.jdbc.JDBCDataSource
connection.url=jdbc:hsqldb:mem:dbtest-local
connection.userName=sa
connection.password=
#POOL
connection.pool.maximumPoolSize=1
connection.pool.idleTimeout=28500
#HIBERNATE
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=H2Dialect
hibernate.show_sql=true
If you use the dataSourceClassName, you should not provide the jdbc url.
Instead, you should add the host, dbname, etc. as properties. See the example on github:
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=test
dataSource.password=test
dataSource.databaseName=mydb
dataSource.portNumber=5432
dataSource.serverName=localhost
Otherwise, you should not use the dataSourceClassName. Try with:
<bean id="hikariConfiguration" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="maximumPoolSize" value="${connection.pool.maximumPoolSize}" />
<property name="idleTimeout" value="${connection.pool.idleTimeout}" />
<property name="jdbcUrl" value="${connection.url}" />
<property name="dataSourceProperties">
<props>
<prop key="user">${connection.userName}</prop>
<prop key="password">${connection.password}</prop>
</props>
</property>
</bean>
This is working for me in local.
What is the difference between using datasource and using hibernateProperties. I want to use c3P0 with spring in my app. I found 2 ways to do so but I'm unable to understand the difference between the two
First:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
depends-on="dataSource">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="maxPoolSize" value="10" />
<property name="numHelperThreads" value="5" />
</bean>
Second:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
>
<property name="hibernateProperties">
<props>
<property name="hibernate.c3p0.maxSize" value="100" />
<property name="hibernate.c3p0.numHelperThreads" value="5" />>
</props>
</property>
</bean>
The first you get a Spring managed datasource, which you can also use for a JdbcTemplate or other work.
The second you get a hibernate managed datasource which is not reusable by Spring.
I strongly suggest the first approach as it also makes it quite easy to replace your datasource for testing (by replacing it with an in-memory database) or to replace it with a JNDI lookup.
I need to create Cron service in Spring, but I can not find enough info how to do it with jdbc store. I want Quartz to use my present connection to Datasource, my database is PostgreSql. I need to create use jdbc store, because task should be done even if server was down.
You can try something like this
<bean id="scheduler-JDBC" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" abstract="true">
<property name="dataSource" ref="myDataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
<property name="overwriteExistingJobs" value="true" />
<property name="quartzProperties">
<props>
<prop key="org.quartz.jobStore.isClustered">false</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
</props>
</property>
</bean>
<bean id="cronScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" parent="scheduler-JDBC">
<property name="startupDelay" value="10" />
<property name="autoStartup" value="true" />
<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
<property name="triggers">
<list>
<ref bean="myTrigger" />
</list>
</property>
</bean>
Download Quartz distribution from http://quartz-scheduler.org/ and you will find the database script for needed tables in docs/dbTables.