Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Change user/pwd on EntityManager in J2EE for auditing(Forcing EclipseLink to use a non-pooled user based connection for updates in JTA for webapps and ejb's)
Change user/pwd on EntityManager in J2EE for auditing [message #1227256] Sat, 04 January 2014 00:48
gregory arrowood is currently offline gregory arrowoodFriend
Messages: 1
Registered: January 2014
Junior Member
First let me say that I am sorta an EclipseLink noob... That is, I've been programming for years in Java, J2ee, etc, however, I have only used Toplink/EclipseLink via Glassfish and normally by using Container Managed Persistance in WebApps and stateless EJB's. I have done some App Managed Persistance and other related things but normally we use Container Managed.

I am porting an older JDBC based web system to persistance and JSF and have been using the default persistance setups (CMP, JTA, connection pool w/common default user). However, we have the necessity to audit a small percentage of our tables where the user can do deletes and updates. This is normally done via an audit table and a trigger which copies the record about to be updated/deleted from the working table to the audit table. The audit table has a couple of fields which get the current timestamp and current user into a couple of fields automatically.

I have googled and searched (mainly glassfish forums) but since my problem is actually in getting EclipseLink to do what I need(I think), I am here now.

In researching this, I have been able to get the Application Managed Persistence (Resource local) to work by defining a default in my persistence.xml file and then by custom configuring the em to the actual user as defined here:


http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing#Example_EntityManager_using_a_user_connection

And according to the wiki noted above, you should be able to do the same thing on the J2EE / JTA environment as long as you modify the em before it is attached to a connection.

Quote:
If a JEE and JTA managed EntityManager is used, specifying a user/password can be more difficult, as the EntityManager and JDBC connection is not under the applications control. The persistence unit properties can still be specified on the EntityManager. As long as this is done before the EntityManager has established a database connection, this will still work.


I have seen this stated and restated in various places, however, nowhere does it state just where to place the code to guarantee that you did it "before the EntityManager has established a database connection".

In my code, I define the default persistence.xml file and the jta datasource setup in glassfish has my default user/pwd.

Note: in the clips below, I had to remove the url's and replace w/***url removed*** because since this is my first msg in these forums I was not allowed to have any links pointing outside eclipse.org domain. Sorry.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="**url removed**" xmlns:xsi="**url removed**" xsi:schemaLocation="**url removed** **url removed**">
  <persistence-unit name="ScottTestUserPwdCMPPU" transaction-type="JTA">
    <jta-data-source>jdbc/ScottTestUserPwdCMPPU</jta-data-source>
    <class>com.davisco.persist.ifx.scope.Rtopsum</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.logging.level" value="FINEST"/>
      <property name="eclipselink.jdbc.exclusive-connection.mode" value="Always"/>
      <property name="eclipselink.jdbc.exclusive-connection.is-lazy" value="true"/>
    </properties>
  </persistence-unit>
</persistence>


The entity facade generated by Netbeans and modified by me contains the following:

@PersistenceContext(unitName = "ScottTestUserPwdCMPPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager()
	{
	logger.log(Level.INFO,"getEntityManager() - overridding user/pwd in em");

	em.setProperty( "eclipselink.logging.level", "FINEST");
	em.setProperty( "javax.persistence.jdbc.user", "scott");
	em.setProperty( "javax.persistence.jdbc.password", "abc123");
	em.setProperty( "eclipselink.jdbc.exclusive-connection.mode", "Always");
	em.setProperty( "javax.persistence.jtaDataSource", "jdbc/ScottTestUserPwdCMPPU");
	em.setProperty( "eclipselink.jdbc.exclusive-connection.is-lazy", "true");

	return em;
	}


And the abstract facade (generated by NB and extended by the entity facade above) contains:

	
public void edit(T entity)
	{
	EntityManager em = getEntityManager();
	em.merge(entity);
	}


And the controller that does the update:

	getFacade().edit(current);


where 'current' is the current object being updated and getFacade() returns the facade above..

When I run my webapp, modify the record and press the submit button, it calls the edit() method above on the facade, which get the entity manager after being customized with the setProperty(), and does the update. This fires my trigger and I get the audit record in the audit table with the default (wrong) user and a newly updated record in the working table. It even looks like the correct user (scott) is going to be used, but just before the update it switches back to the dafault user so the audit has the default user in it. I have some of the log messages below, with some removed for brevity. I will be glad to send any code or logs as necessary but did not know what you actually need to troubleshoot this so I'm trying to send a little of everything without loading down this forum.

"scott" below is the correct user for the write.
"davisco" below is the default user attached to the read connection pool.


[#|2013-12-31T22:37:22.411+0000|CONFIG|glassfish3.1.2|org.eclipse.persistence.session.file:/davisco/glassfish/glassfish/domains/domain1/applications/ScottTestUserPwdCMP/WEB-INF/classes/_ScottTestUserPwdCMPPU.connection|_ThreadID=24;_ThreadName=Thread-2;|connecting(DatabaseLogin(
        platform=>InformixPlatform
        user name=> "scott"
        connector=>JNDIConnector datasource name=>jdbc/ScottTestUserPwdCMPPU
))|#]



Then immediately following that, It seems to 'switch' back to the default user below.

[#|2013-12-31T22:37:22.412+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=getTransaction;|TM: getTransaction: tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=null localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f], tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]

[#|2013-12-31T22:37:22.413+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=getTransaction;|TM: getTransaction: tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=null localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f], tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]

[#|2013-12-31T22:37:22.413+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.JavaEETransactionManagerSimplified;MethodName=enlistResource;|

In JavaEETransactionManagerSimplified.enlistResource, h=958 h.xares=com.sun.gjc.spi.XAResourceImpl@1031d83c tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=null localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f]|#]

[#|2013-12-31T22:37:22.414+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=setTransactionManager;|TM: setTransactionManager: tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]

[#|2013-12-31T22:37:22.414+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.JavaEETransactionManagerSimplified;MethodName=enlistXAResource;|TM: enlistResource|#]

[#|2013-12-31T22:37:22.415+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.JavaEETransactionImpl;MethodName=enlistResource;|--In JavaEETransactionImpl.enlistResource, jtsTx=com.sun.jts.jta.TransactionImpl@8fe14195 nonXAResource=null|#]

[#|2013-12-31T22:37:22.415+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.JavaEETransactionImpl;MethodName=registerSynchronization;|--In JavaEETransactionImpl.registerSynchronization, jtsTx=com.sun.jts.jta.TransactionImpl@8fe14195 nonXAResource=null|#]

[#|2013-12-31T22:37:22.416+0000|CONFIG|glassfish3.1.2|org.eclipse.persistence.session.file:/davisco/glassfish/glassfish/domains/domain1/applications/ScottTestUserPwdCMP/WEB-INF/classes/_ScottTestUserPwdCMPPU.connection|_ThreadID=24;_ThreadName=Thread-2;|Connected: null
        User: davisco
        Database: Informix Dynamic Server  Version: 9.54.FC4
        Driver: IBM Informix JDBC Driver for IBM Informix Dynamic Server  Version: 2.21.JC5|#]

[#|2013-12-31T22:37:22.416+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=getTransaction;|TM: getTransaction: tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=com.sun.jts.jta.TransactionImpl@8fe14195 localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f], tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]


Then immediately after that, the actual update statement:


[#|2013-12-31T22:37:22.416+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=getTransaction;|TM: getTransaction: tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=com.sun.jts.jta.TransactionImpl@8fe14195 localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f], tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]

[#|2013-12-31T22:37:22.417+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.JavaEETransactionManagerSimplified;MethodName=delistJTSResource;|TM: delistResource|#]

[#|2013-12-31T22:37:22.417+0000|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file:/davisco/glassfish/glassfish/domains/domain1/applications/ScottTestUserPwdCMP/WEB-INF/classes/_ScottTestUserPwdCMPPU.connection|_ThreadID=24;_ThreadName=Thread-2;ClassName=null;MethodName=null;|reconnecting to external connection pool|#]

[#|2013-12-31T22:37:22.418+0000|FINE|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=24;_ThreadName=Thread-2;ClassName=com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate;MethodName=getTransaction;|TM: getTransaction: tx=JavaEETransactionImpl: txId=31 nonXAResource=null jtsTx=com.sun.jts.jta.TransactionImpl@8fe14195 localTxStatus=0 syncs=[com.sun.ejb.containers.ContainerSynchronization@335b61e5, org.eclipse.persistence.transaction.JTASynchronizationListener@4c6e5c2f], tm=com.sun.jts.jta.TransactionManagerImpl@53a70c41|#]

[#|2013-12-31T22:37:22.419+0000|FINE|glassfish3.1.2|org.eclipse.persistence.session.file:/davisco/glassfish/glassfish/domains/domain1/applications/ScottTestUserPwdCMP/WEB-INF/classes/_ScottTestUserPwdCMPPU.sql|_ThreadID=24;_ThreadName=Thread-2;ClassName=null;MethodName=null;|UPDATE rtopsum SET osm_comp_bank = ?, osm_dollars = ?, osm_minutes_comped = ? WHERE ((((((osm_date = ?) AND (osm_rec_type = ?)) AND (osm_job = ?)) AND (osm_dept = ?)) AND (osm_empno = ?)) AND (osm_code = ?))
        bind => [9 parameters bound]|#]



The problem may be something very stupid I am doing, or just some configuration oversight. I am also somewhat confused between the difference in JTA and JPA so I might be mixing methods without realizing it. But any help would be appreciated. If you need any more code or logs, just let me know.

My environment is as follows:
EclipseLink Version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
Glassfish Version : 3.1.2 running on Linux

Any assistance is appreciated.
Gregory Arrowood.
Previous Topic:H2 with EclipseLink
Next Topic:Toplink 9 to EclipseLink migration issue
Goto Forum:
  


Current Time: Sat Apr 04 16:28:38 GMT 2020

Powered by FUDForum. Page generated in 0.01880 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top