BoneCP sometimes cannot get config from spring property holder - spring

I have been facing this issue for a while now. My config is as following
<!-- Load Properties Files -->
<context:property-placeholder location="classpath:*-${environment}.properties" ignore-unresolvable="true"/>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="idleConnectionTestPeriodInMinutes" value="${boneCP.idleConnectionTestPeriodInMinutes}"/>
<property name="idleMaxAgeInMinutes" value="${boneCP.idleMaxAgeInMinutes}"/>
<property name="maxConnectionsPerPartition" value="${boneCP.maxConnectionsPerPartition}"/>
<property name="minConnectionsPerPartition" value="${boneCP.minConnectionsPerPartition}"/>
<property name="partitionCount" value="${boneCP.partitionCount}"/>
<property name="acquireIncrement" value="${boneCP.acquireIncrement}"/>
<property name="statementsCacheSize" value="${boneCP.statementsCacheSize}"/>
<property name="lazyInit" value="true"/>
<property name="maxConnectionAgeInSeconds" value="${boneCP.maxConnectionAgeInSeconds}"/>
</bean>
The project is running on Tomcat 7
On the local machine, the project deploy with no error as well as for the dev server. Unfortunately, the project cannot be deployed on dev server any more (server configuration remain same) while local machine is still fine. Every time I deploy the project on the dev server, Tomcat just hang at INFO: Deploying web application archive /etc/tomcat/webapps/project.war. But if I config BoneCP with real values, everything is fine.
Could any one tell me what's wrong with it?
It turned out to be the lazyInit problem. If I comment it out, the server can start normally. But now I'm facing the new issue though. Mybatis cannot access the db at all while the local machine is 100% fine. and yet dont throw any exception. But when I stop the server, I found the following exceptions
INFO: Illegal access: this web application instance has been stopped already. Could not load com.jolbox.bonecp.PoolUtil. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at com.jolbox.bonecp.DefaultConnectionStrategy.getConnectionInternal(DefaultConnectionStrategy.java:94)
at com.jolbox.bonecp.AbstractConnectionStrategy.getConnection(AbstractConnectionStrategy.java:90)
at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:540)
at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.java:131)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:66)
AND
INFO: Illegal access: this web application instance has been stopped already. Could not load org.apache.ibatis.reflection.ExceptionUtil. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:363)
at sun.proxy.$Proxy15.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:195)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:124)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
at sun.proxy.$Proxy45.selectByExample(Unknown Source)

Well it could be many things but its most likely
properties are not being replaced with the values you think
the database number of connections has been exceeded or the wrong host
A combination of 1 + 2
For #1 I would a make a separate bean that needs com.jolbox.bonecp.BoneCPDataSource as a dependency and have it print out the getters of BoneCPDataSource.
For #2 I would turn on as much logging as possible (see log4j or logback or whatever your logging framework is).

Related

Handle Spring data source being temporarily unavailable during Hibernate initialisation

Part of my Spring (4.3.23) / Hibernate (5.0.12) application "A" uses a data source that is exposed be a second application "B" (the data source is an in-memory database). Both A and B are deployed in Tomcat and I don't control start order. Once both A and B have started both behave as expected, however if A starts before B an error is thrown during initialisation when Hibernate tries to query the data source:
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : Cannot create PoolableConnectionFactory (Connection is broken: "java.net.ConnectException: Connection refused: 127.0.1.1:5521" [90067-199])
Is there any way I can suppress this error, delay this part of initialisation, or tell Hibernate that the data source may not be immediately available?
Here are the relevant parts of my configuration:
<bean id="memDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/memdb" />
</bean>
<bean
id="memSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="memDataSource" />
<property name="packagesToScan" value="com.company" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
<bean id="memTransactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="memSessionFactory" />
<qualifier value="memTransactions"/>
</bean>
<tx:annotation-driven transaction-manager="memTransactionManager" />
I would wind up scripting the deployment overall. Bring up tomcat with application A in the webapps directory, and periodically ping for successful deployment of application A before copying/moving over application B into the webapps directory for deployment.
This entire solution would likely work best if you used a staging directory to move your war files onto that server, and then let the script clear the webapps directory and move/copy the new war(s) into webapps for fresh deployment.
SideNote: I've found that deployment of applications seems to be in a constant order, but I'm not sure as it is alphabetical, oldest first, or something else.

EJB remote call from standalone client to JBoss server is not working in Multi threading

I am using spring container in a batch operation that realize many calls to an remote EJB. When the operation is single-thread everything works fine but when try to use multi-threading to gain performance it throws:
Exception in thread "taskExecutor-1" javax.ejb.EJBException: java.io.IOException: Channel Channel ID e9c80c0d (outbound) of Remoting connection 18f42160 to servername/ip:port has been closed
My stand-alone cliente dependecy is:
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
<type>pom</type>
<version>7.2.0.Final</version>
</dependency>
I'm also using spring's SimpleRemoteStatelessSessionProxyFactory to inject the lookup beans
<bean id="jndiProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:/path/jboss-ejb-client.properties" />
</bean>
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment" ref="jndiProperties" />
</bean>
<bean id="operation" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="ejb:remote/interface/location" />
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="businessInterface" value="com.MyBussinesInterfae" />
</bean>
In my tests I verified that if a Thread-A creates the InitContext and does the JNDI EJB lookup and creates the instance of the EJB remote inferface (under the SRSSPFBean) it can invoke methods via the EJB remote interface, but if an Thread-B gets the reference of the EJB remote interface and tries to invoke a method the exception is thrown.
The only similiar problem found on my search is here and seems related to bugs on Jboss AS 7. I am using Jboss eap 6.2.
Thanks in advance for any help.

Failing to connect to database some times spring-jdbc, commons-dbcp,tomcat

I am using spring3 and org.apache.commons.dbcp.BasicDataSource and apache tomcat 6.x.
Once the application is started it works fine but after some time it fails to connect to database... on restart it connects again.
below is configuration used in applicationContect.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:initialSize="5" p:maxActive="15" p:maxIdle="5" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://<Host>/<schema>?useUnicode=true&" p:username="<uName>" p:password="<pwd>" p:testOnBorrow="true" p:validationQuery="SELECT 1"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
Can there be any problem with the configuration?
Also, i am not closing/handling jdbc connections/resultset and i assume spring-jdbc will take care of it .
I am getting the below exception:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, general error
executing stored procedures with jdbcTemplate.query was failing some times.
Instead i used org.springframework.jdbc.core.simple.SimpleJdbcCall for calling stored procedures and its working fine

Spring JNDI datasource not recognized after upgrade to ActiveMQ 5.6.0

I tested a ActiveMQ 5.5.0 (fuse version) app in AMQ 5.6.0 and noticed that our Spring JNDI configured Oracle datasources aren't being found.
The only thing I changed in my applications was the pom.xml versions of AMQ/Spring (to match the 5.6 versions). Otherwise, I'm using the identical application code and configuration (activemq.xml, jndi.xml, etc), but my Spring JDBC DAOs (v3.0.5) are failing to find them.
No errors in the logs otherwise, just this Spring Application Context initialization error...
javax.naming.NameNotFoundException; remaining name 'jdbc/myDataSource'
here is the relevant Spring jndi config (conf/jndi.xml, included in conf/activemq.xml)...
<bean id="jndi" class="org.apache.xbean.spring.jndi.SpringInitialContextFactory"
factory-method="makeInitialContext" scope="singleton">
<property name="entries" ref="jndiEntries" />
</bean>
<util:map id="jndiEntries">
<entry key="jdbc/myDataSource">
<bean id="myDBCPDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
...
then my application references it like this...
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/myDataSource</value>
</property>
</bean>
<bean id="messageDAO" class="com.mycompany.MessageDAOImpl">
<property name="dataSource" ref="myDataSource" />
</bean>
That said, I tested without using JNDI (instead just hardcoded the datasource in my app) and everything works as expected. So that should rule out everything except the Spring JNDI registration/lookup of the datasource, etc.
So, what am I missing?
ActiveMQ has a dependency into xbean-spring, which you are using as a JNDI provider. It is likely that the transitive Xbean dependency has changed because of the upgrade to ActiveMQ 5.6.0.
I found the issue, I added a jndi.properties file under the /conf directory containing the following and it works fine now (didn't need this under AMQ 5.5...strange)...
java.naming.factory.initial = org.apache.xbean.spring.jndi.SpringInitialContextFactory

Error when starting Mule-ESB Mx4j agent in WebSphere 7

I'm trying to use an Mx4j agent (and Spring Framework 3.0.5) to expose some POJOs in my Mule-ESB (Mule 3.1.2) application as an HTTP service. The agent is configured in mule-config.xml as follows:
<management:jmx-mx4j-adaptor jmxAdaptorUrl="http://0.0.0.0:9990" />
Also, I use the Spring MBeanExporter to expose the desired POJOs:
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="assembler" ref="assembler" />
<property name="namingStrategy" ref="namingStrategy" />
<property name="autodetect" value="true" />
</bean>
<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="jmxAttributeSource" />
</bean>
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="jmxAttributeSource" />
</bean>
Everything works fine in my desktop environment, which runs in Jetty. When I deploy the EAR to our WebSphere 7 Server, however, the application doesn't start, raising the following exception:
[3/30/12 16:33:58:858 BRT] 00000038 webapp I com.ibm.ws.webcontainer.webapp.WebApp log SRVE0296E: [BaseApp#BaseApp.war][/context][Servlet.LOG]:.Failed to invoke lifecycle phase "start" on object: org.mule.module.management.agent.Mx4jAgent#13ef13ef:.org.mule.api.lifecycle.LifecycleException: Failed to invoke lifecycle phase "start" on object: org.mule.module.management.agent.Mx4jAgent#13ef13ef
at org.mule.lifecycle.phases.DefaultLifecyclePhase.applyLifecycle(DefaultLifecyclePhase.java:236)
at org.mule.lifecycle.RegistryLifecycleManager$RegistryLifecycleCallback.onTransition(RegistryLifecycleManager.java:276)
...
...
Caused by: org.mule.module.management.agent.JmxManagementException: Failed to start Mx4j agent
at org.mule.module.management.agent.Mx4jAgent.start(Mx4jAgent.java:205)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.mule.lifecycle.phases.DefaultLifecyclePhase.applyLifecycle(DefaultLifecyclePhase.java:225)
... 67 more
Caused by: javax.management.InstanceNotFoundException: Mule.BaseApp.6:name=Mx4jHttpAdapter
at java.lang.Throwable.<init>(Throwable.java:67)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBeanInfo(DefaultMBeanServerInterceptor.java:1384)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getMBeanInfo(JmxMBeanServer.java:892)
at com.ibm.ws.management.AdminServiceImpl.getMBeanInfo(AdminServiceImpl.java:1524)
at com.ibm.ws.management.AdminServiceImpl.checkForOpDeprecation(AdminServiceImpl.java:2662)
at com.ibm.ws.management.AdminServiceImpl.preInvoke(AdminServiceImpl.java:2284)
at com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1309)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1225)
at com.ibm.ws.management.PlatformMBeanServer.invoke(PlatformMBeanServer.java:743)
at org.mule.module.management.agent.Mx4jAgent.start(Mx4jAgent.java:201)
Looking at the second (and last) stack trace cause, there seem to be some confusion or assumption about the JMX Server Instance Name. However, I have no control on that (and I can't figure why it works fine in Jetty and not in WAS 7).
Has anyone stepped through that before? Am I doing something wrong? Am I missing something here?
Thanks a lot for any response!
For WAS using JMX is not that straightforward. Complications are:
Security - one can't access JMX endpoint by default without further security configuration (out of scope for this discussion).
Websphere's JMX server implementation actually modifies MBean domain names to be prefixed with WAS Node and Cell. There is a WAS-specific API that can get you those, but the takeaway is this can't be done without further coding.
Andrew

Resources