Connections obtained from Weblogic are set to autocommit - java

We have an application developed using Spring 3.1.1 and Hibernate 3.6.10, and uses Oracle 11g as database.
The application looks-up the datasource using JNDI, and uses a org.springframework.orm.hibernate3.HibernateTransactionManager.
And spring configuration file contains:
<jee:jndi-lookup jndi-name="jdbc/ds"
id="dataSource" proxy-interface="javax.sql.DataSource"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
....
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
We use Tomcat in development machines, but the application is deployed on Weblogic 11 for production.
The problem is in Weblogic datasource.log file, the following message is logged:
<autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=false,doInit=false,'null',destroyed=false,poolname=life_rac,appname=null,moduleName=null,connectTime=45,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1531636426817,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:356)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:364)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:330)
...
Now we are worried about that autoCommit=true in the log messages, cause it seems that in some cases the first DML queries are committed when they shall not, especially when the Weblogic is stopped due to some problems (for example power-loss).
I shall mention that the data-source defined in Weblogic Server is a XA-Datasource.
Is there a way to change default configuration of autoCommit to false?
As we have just one database, and no other transactional resources, I believe we change can the datasource to a non-XA one, without any impact. Am I correct?
And we need to change the configuration so it works both on Weblogic and Tomcat (adding some configuration to Tomcat -such as adding a transaction manager- is okay).

WebLogic Server 11g allows you to create and deploy a jdbc interceptor class. This documentation will help to write the class. Each time a connection is created, your interceptor will be called and you will be able to set the auto commit property to false.
This way is deprecated in 11g. 12c uses a Connection Initialization Callback.

Related

How to change the TaskExecutor implementation dynamically depending on the application server

I am using Spring 3.0.x to get a WorkManager from an application server and use it to run scheduled jobs that need database access. The problem is, is that this application could be deployed to different Java application servers - in this case, to Websphere 7.0 and GlassFish 3.1 OSE. (Crazy combo, I know...)
The problem I am having is that before deploying to either, I have to change the bean to reference the appropriate TaskExecutor class used for each server. I have Spring load up an applicationContext.xml with these beans in it.
For Websphere, I have to pull the WorkManager externally and use the following bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="wm/default" />
<property name="resourceRef" value="true"/>
</bean>
and the web.xml has something like this:
<resource-ref>
<description>WorkManager</description>
<res-ref-name>wm/default</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
For GlassFish, I can just use the bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
</bean>
Can I somehow dynamically change which TaskExecutor implementation is used on the fly?
I could easily check for the Websphere resource to determine if I am on Websphere, then fall back to GlassFish. But how to load a class like that with Spring (using Annotations or otherwise) is baffling me.
Any help is appreciated. Thanks!
P.S. I am not worried about being J2EE compliant for GlassFish - it's just that Websphere forces you to be so (hence pulling an external WorkManager)
You can easily use a custom VM argument to determine what environment you are running in, and use that to load the appropriate context file.
One file for each supported environment, all of which define the same bean.
task-exec-was.xml
task-exec-glassfish.xml
Add a required VM argument.
-Dapp.server=was
Then, wherever you actually load the bean you include the appropriate file based on a PropertyPlaceholderConfigurer.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<import resource="task-exec-${app.server}.xml" />
Edits based on information in the comments.
You can override for the environment you do have control over in this case.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="classpath:spring/was.properties/>
</bean>
<import resource="task-exec-${app.server}.xml" />
Now you have a file called spring/was.properties that defines a property app.server=was, which is read by default. In Glassfish, you supply the same property as a VM argument, which will now override the property read from the file.

Can't see JMX entries in jconsole when using Tomcat JDBC Connection Pool

we're evaluating switching over from the C3P0 connection pool to the Tomcat JDBC Connection Pool (as described here).
It appears to work as a connection pool but I can't seem to see any JMX entries for it when I run jconsole.
Out of the box C3P0 gives lots of operations and attributes via JMX, the Tomcat
JDBC Connection Pool gives none (for me).
According to the page linked above there is a jmxEnabled flag that defaults to true. I've set this explicitly but it seems to make no difference.
What am I missing?
I'm running a fairly standard Java6/Spring/Hibernate app by the way.
If you configure pool in your spring context, you should export bean manually. Autoexport works only if you confiugre pool in tomcat container and import it from JNDI. See http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#JMX
You may use this spring config for export pool information to JMX:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
... skipped ...
</bean>
<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=DataSource" value="#{dataSource.getPool().getJmxPool()}"/>
</map>
</property>
</bean>
Config works only in Spring version 3.0 and above, because it uses spring expression language
Do you see any entries under the tree
Catalina -> DataSource -> javax.sql.DataSource
This lists the number of active connections, idle connections, and a few other common stats.
Other then that, what kind of information are you hoping to get out of monitoring?
Darren, if you don't see the object name under Catalina/DataSource/javax.sql.DataSource/<name> in JConsole, then I wonder if the datasource is defined incorrectly or perhaps it was not connected to the database when Tomcat was started.
Bruce
In support of Sean's post:
The placement of the javax.sql.DataSource entry in MBeans of JConsole is:
Catalina
DataSource
/[Name_of_deployed_application] // e.g. "/DomainService
/[Host_name_of_the_deployed_application] //e.g. "localhost"
javax.sql.DataSource

TransactionProxyFactoryBean when switching from configuration-based Service beans to annotation based service beans

I read about using
<context:component-scan base-package="tld.mydomain.business">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
and annotate my service beans with #Service("myService"), and thought great, I'll do that, since I'm already doing that with my controllers. My usual service bean configuration looks like
<bean id="userService" parent="txProxyTemplate">
<property name="target">
<bean class="tld.mydomain.business.UserServiceImpl"/>
</property>
<property name="proxyInterfaces" value="tld.mydomain.business.UserService"/>
</bean>
so now that I generate them, how do I wrap them in a Hibernate proxy such as TransactionProxyFactoryBean? Or is there a better way to do that as well?
I have not yet gone all the way and used #Repository as well, is that required?
Cheers
Nik
Using TransactionProxyFactoryBean is not encouraged in modern Spring applications, although it still works. The typical approach nowadays is to annotate classes with #Transactional, and then stick this element in your application context file:
<tx:annotation-driven transaction-manager="txManager"/>
This and other strategies are discussed in great depth in the reference document, and there's even a side note about TransactionProxyFactoryBean.
There's no need for
<context:include-filter type="annotation"expression="org.springframework.stereotype.Service"/>
Spring will register #Service, #Repository, #Component... once they are found in the base package.
Like #Rob said either use #Transactional or <aop:config>...</aop:config> to handle your transactions at the service level.
If you have two different resources that need to be in the same transaction, then you will need to use JTA. See my answer to an earlier question here. Your config would need to look something like:
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="appserver/jndi/path" />
</bean>
Where appserver/jndi/path would need to be replaced with the JNDI path of the JTA transaction manager that comes with your application server (although you can use a standalone JTA transaction manager such as JOTM as well). Typical paths as mentioned in the 2.5.x API are:
"java:comp/UserTransaction" for Resin 2.x, Oracle OC4J (Orion), JOnAS (JOTM), BEA WebLogic
"java:comp/TransactionManager" for Resin 3.x
"java:appserver/TransactionManager" for GlassFish
"java:pm/TransactionManager" for Borland Enterprise Server and Sun Application Server (Sun ONE 7 and later)
"java:/TransactionManager" for JBoss Application Server

Hibernate MySQL transaction configuration issue

I'm having trouble starting a transaction with Hibernate and MySQL while running in JUnit. I'm getting a HibernateException which states: "No TransactionManagerLookup specified". I believe this error is because I don't have a proper configuration setting for hibernate.transaction.manager_lookup_class.
I see that under the namespace of org.hibernate.transaction there are quite a few different lookup classes that I could use. All of the documentation that I could find on these was very vague. My question is what is the appropriate one for MySQL?
I do it with Spring and its transaction managers. Works perfectly.
To fix this I needed to make the following changes.
Changed the hibernate.cfg.xml => hibernate.current_session_context_class from jta to thread.
Changed the transaction manager to
org.springframework.orm.hibernate3.HibernateTransactionManager
in the bean configuration.
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" >
<property>
<bean>

how do I change persistence.xml at run time

I am new to openJPA.
I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml.
For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.
And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.
FYI I am using WS app. server 7 and RAD 7.5
Any type of help would be highly appreciated.
You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.
Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.
Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.
For example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:#localhost:1521:XE
Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.
Of course, you can use JNDI data sources with Spring also.

Categories

Resources