1

We are using spring data combined with JNDI lookup to get hold of datasource and establish a connection.

<bean id="testDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/testDs"/>
</bean>

I am looking for a solution to write wrapper before getting DB connection, so that I can log time either in logs or inserting a new record, block few transactions to few tables based on insert/delete/update statements.

Currently, we are not thinking of using database triggers. Any help/advice/example would be greatly appreciated.

jslearner07
  • 451
  • 1
  • 4
  • 17

1 Answers1

0

You could create your own DataSource class by extending BasicDataSource and add logging to the constructor. You should be able to inject your JNDI data source into your new data source class.

Grayson
  • 591
  • 1
  • 4
  • 17
  • Is there any class available with Spring which I can extend to achieve above functionality. Please advice – jslearner07 May 25 '16 at 18:44
  • You could use the following: org.apache.commons.dbcp.BasicDataSource – Grayson May 25 '16 at 20:56
  • I tried using the way you told. Although I am able to log connection opening time, but I am not able to get hold of sql statments executed. I used the below method as per documentation getConnectionInitSqls(). But its not working. Any idea how to log sql statments?. – jslearner07 May 26 '16 at 07:34
  • You will have to extend JdbcTemplate and overload the methods to include your logging. Another alternative is to use AOP to perform your logging. I am not an AOP expert so you may want to re-ask your question with AOP in mind. – Grayson May 27 '16 at 21:31
  • Thanks I used dynamic proxy to log sql statements. Below is the link i followed. http://stackoverflow.com/questions/4691521/how-to-get-parameters-from-preparedstatement – jslearner07 Jun 07 '16 at 12:05