Running simple SELECT MS SQL Server query with JDBCTemplate using JTDS driver and c3p0 data source - spring

I am connecting to MS SQL server with following datasource
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" >
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:jtds:sqlserver://<server>:<port>" />
<property name="user" value="${echo_db_user}" />
<property name="password" value="${echo_db_password}" />
</bean>
<bean id="myProviderDAO" class="com.care.dao.impl.DataProviderImpl">
<property name="dataSource" ref="myDataSource" />
</bean>
public class DataProviderImpl extends JdbcDaoSupport{
public Object runQuery(String staticQuery) {
try {
return getJdbcTemplate().queryForList(staticQuery);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
staticQuery is:
Select
sender,
status_dt
FROM
ServiceTickets.dbo.trouble_ticket
WHERE
status_dt BETWEEN '02-17-2010 07:00:00' AND '04-30-2014 05:00:00'
When running this query I am getting following exception:
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [
Select
sender,
status_dt
FROM
ServiceTickets.dbo.trouble_ticket
WHERE
status_dt BETWEEN '02-17-2010 07:00:00' AND '04-30-2014 05:00:00'
]; nested exception is java.sql.SQLException: ORA-00933: SQL command not properly ended
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
Connection is happening as I have debugged the code and it passed where connection is retrieved.
Same query If I execute on Toad (client to ms sql server) executes properly.
I have used same set of steps to connect to Qracle DB and executing its queries and it works fine, only in case of MS SQL server I am facing this issue.

Related

java.lang.Security Exception

i implemented spring application which is running based on schedulers in weblogic 10. server.
while i am deploying it. i am getting above exception.
here is my stack trace
java.lang.SecurityException: [Security:090398]Invalid Subject: principals=[bpm_weblogic, AdminChannelUsers, Administrators, AppTesters, CrossDomainConnectors, Deployers, Monitors, Operators, OracleSystemGroup]
at weblogic.security.service.SecurityServiceManager.seal(SecurityServiceManager.java:833)
at weblogic.security.service.IdentityUtility.authenticatedSubjectToIdentity(IdentityUtility.java:30)
at weblogic.security.service.RoleManager.getRoles(RoleManager.java:183)
at weblogic.security.service.AuthorizationManager.isAccessAllowed(AuthorizationManager.java:375)
at weblogic.jndi.internal.ServerNamingNode.checkPermission(ServerNamingNode.java:442)
at weblogic.jndi.internal.ServerNamingNode.checkLookup(ServerNamingNode.java:423)
at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:180)
at weblogic.jndi.internal.BasicNamingNode.unbind(BasicNamingNode.java:565)
at weblogic.jndi.internal.WLEventContextImpl.unbind(WLEventContextImpl.java:173)
at javax.naming.InitialContext.unbind(InitialContext.java:435)
at com.tcs.controller.BpmController.run(BpmController.java:94)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
my spring application is running in one weblogic environment and i am calling bpm which is running in another weblogic environment.
here if don't call bpm i am not getting exception and if i user dataSource instead of jndi it is working fine.
but the problme is i have to call the bpm and i can't user dataSource configuration in production.
here is my controller class
public String executeBpm(User user) {
IBPMContext context = null;
String status = null;
if (logger.isDebugEnabled()) {
logger.debug("executeBpm method starts");
}
try {
if (user.getUserId() != null && !("").equals(user.getUserId())) {
context = ITBABPMContext.getIBPMContextUsingName(user.getUserId());
}
HashMap<String, Object> elements = (HashMap<String, Object>) user.getMap();
UpdateTaskDetails updates = new UpdateTaskDetails();
if (user.getTaskId() != null && !("").equals(user.getTaskId())) {
updates.setTaskID(user.getTaskId());
}
if (user.getTaskId() != null && !("").equals(user.getTaskId())) {
updates.setTaskOutcome(user.getTaskOutcome());
}
if (user.getUserComment() != null && !("").equals(user.getUserComment())) {
updates.setUserComment(user.getUserComment());
}
if (!elements.isEmpty() && elements.size() > 0) {
updates.setElementList(elements);
}
if (logger.isDebugEnabled()) {
logger.debug("executeBpm method ends");
}
status = ITBAACMUtil.updateTaskOutcome(updates, context);
if (status.equalsIgnoreCase("success")) {
bpmProcessorService.write(user.getUserId(), user.getSeqNo());
}
} catch (ITBABPMRuntimeException e) {
e.printStackTrace();
} catch (BPMServiceClientException e) {
e.printStackTrace();
} catch (BPMException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return status;
}
my dao class
public void write(String userId,Long seqNo){
try{
String query=messageSource.getMessage(BPMConstants.FAILED_QUERY,new Object[]{Long.toString(seqNo)},Locale.US);
jdbcTemplate.update(query);
}catch(Exception e){
logger.error("exception at updating the status to failed ..");
logger.error(e.getStackTrace());
}
}
here one thing is cross domain mapping is are already there and other applications are running just fine. so i don't think this is the issue with cross domain mapping.
here is my configuration file.
<context:component-scan base-package="com.app" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="MCDataSource"/>
</bean>
<bean id="txManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager" />
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
<property name="transactionManagerName" value="javax.transaction.TransactionManager"/>
</bean>
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#172.19.8.159:1521/OIM.itba.gov.in" />
<property name="username" value="AppDB"></property>
<property name="password" value="AppDB"></property>
<property name="initialSize" value="2" />
<property name="maxActive" value="5" />
</bean> -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="runScheduler" class="com.app.controller.BpmController" />
<task:scheduled-tasks>
<task:scheduled ref="runScheduler" method="run" cron="0 0/5 * * * ?" />
</task:scheduled-tasks>
Hey it seems your problem is you are not switching the context correctly. Once you open up the IBPM context then you are coming back and trying to lookup on your local jndi tree but you have IBPM context credentials which do not authenticate on your local weblogic. You need to open a new context before you perform a jndi lookup operation to your datasource.
Here is some sample code:
jndiTemplate(org.springframework.jndi.JndiTemplate) { bean ->
bean.scope = "prototype"
environment = [
"java.naming.factory.initial":"weblogic.jndi.WLInitialContextFactory",
"java.naming.security.principal" : "username",
"java.naming.security.credentials": "password"
]
}
dataSource(org.springframework.jndi.JndiObjectFactoryBean){
jndiTemplate = ref(jndiTemplate)
jndiName = "name"
exposeAccessContext=true
}
Then when you want to lookup the jndi you can something like.
Inject the jndiTemplate and:
jndiTemplate.context.getProperties()
Or
InitialContext initialContext = new InitialContext(jndiTemplate.getEnvironment());
You can then close it.

TransactionTemplate and TransactionManager connection objects

The following code in my DAO works perfectly fine.
public void insert(final Person person) {
transactionTemplate.execute(new TransactionCallback<Void>() {
public Void doInTransaction(TransactionStatus txStatus) {
try {
getJdbcTemplate().execute("insert into person(username, password) values ('" + person.getUsername() + "','" + person.getPassword() + "')");
} catch (RuntimeException e) {
txStatus.setRollbackOnly();
throw e;
}
return null;
}
});
}
Following is my spring config.
<bean id="derbyds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="username" value="app" />
<property name="password" value="app" />
<property name="url" value="jdbc:derby:mytempdb" />
</bean>
<bean id="persondaojdbc" class="com.napp.dao.impl.PersonDaoJdbcImpl">
<property name="dataSource" ref="derbyds" />
<property name="transactionTemplate">
<bean class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="derbyds"/>
</bean>
What i wanted to know is how does the
<bean id="persondaojdbc" class="com.napp.dao.impl.PersonDaoJdbcImpl">
<property name="dataSource" ref="derbyds" />
<property name="transactionTemplate">
<bean class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="derbyds"/>
</bean>
Now, its imperative that both the TransactionManager and the code (in my case jdbc template) operate on the same connection. I am assuming both of them are getting the Connection objects from the DataSource. DataSource pools connections and its a chance when you call getConnection multiple times, you will get different Connection obejcts. How does spring make sure that the TransactionManager and JdbcTemplate end up getting the same connection objects. My understanding is that, if that doesn't happen, rollbacks, or commits wont work, correct? Could someone throw more light on this.
If you look at the code for JdbcTemplate (one of the execute(...) methods) you will see
Connection con = DataSourceUtils.getConnection(getDataSource());
Which tries to retrieve a Connection from a ConnectionHolder registered with a TransactionSynchronizationManager.
If there is no such object, it just gets a connection from the DataSource and registers it (if it is in a transactional environment, ie. you have a transaction manager). Otherwise, it immediately returns the registered object.
This is the code (stripped of logs and stuff)
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
conHolder.requested();
if (!conHolder.hasConnection()) {
conHolder.setConnection(dataSource.getConnection());
}
return conHolder.getConnection();
}
// Else we either got no holder or an empty thread-bound holder here.
Connection con = dataSource.getConnection();
// flag set by the TransactionManager
if (TransactionSynchronizationManager.isSynchronizationActive()) {
// Use same Connection for further JDBC actions within the transaction.
// Thread-bound object will get removed by synchronization at transaction completion.
ConnectionHolder holderToUse = conHolder;
if (holderToUse == null) {
holderToUse = new ConnectionHolder(con);
}
else {
holderToUse.setConnection(con);
}
holderToUse.requested();
TransactionSynchronizationManager.registerSynchronization(
new ConnectionSynchronization(holderToUse, dataSource));
holderToUse.setSynchronizedWithTransaction(true);
if (holderToUse != conHolder) {
TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
}
}
return con;
You'll notice that the JdbcTemplate tries to
finally {
DataSourceUtils.releaseConnection(con, getDataSource());
}
release the Connection, but this only happens if you're in a non-transactional environment, ie.
if it is not managed externally (that is, not bound to the thread).
Therefore, in a transactional world, the JdbcTemplate will be reusing the same Connection object.

DbUnit H2 in memory db with Spring and Hibernate

Hi I'm trying a little POC with JPA and unit test to verify that the DB schema is created. I'm working with H2 DB and I set to Hibernate create the schema from the entities, but when DbUnit tries to initialize the DB from a dataset I always get a Table ... not found in tableMap. I read that I have to add the property DB_CLOSE_DELAY=-1 to DB URL but is like after Hibernate creates the schema the DB is losted when DbUnit tries to initialize.
Any ideas? Any help is highly appreciated.
This is my config:
application-context.xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceH2" />
<property name="packagesToScan" value="com.xxx.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
<!-- property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" /-->
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
<!-- property name="database" value="MYSQL" /-->
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="javax.persistence.validation.mode">CALLBACK</prop>
</props>
</property>
</bean>
<bean id="dataSourceH2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
RepositoryTest.java
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/application-context-test.xml" })
#Transactional
public class SystemEntityRepositoryH2Test {
#Inject
private SystemEntityRepository repository;
#Inject
private DataSource dataSourceH2;
private IDatabaseConnection connection;
#Before
public void setUp() throws Exception {
IDatabaseConnection dbUnitCon = null;
dbUnitCon = new DatabaseDataSourceConnection(dataSourceH2, "testdb");
dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);
}
#After
public void tearDown() throws Exception {
//DatabaseOperation.DELETE_ALL.execute(this.getConnection(), this.getDataSet(dataSetFile));
}
#Test
public void test() throws Exception {
}
protected IDataSet getDataSet(String dataSetFile) throws Exception {
ResourceLoader resourceLoader = new ClassRelativeResourceLoader(this.getClass());
Resource resource = resourceLoader.getResource(dataSetFile);
if (resource.exists()) {
return new FlatXmlDataSetBuilder().build(resource.getInputStream());
}
return null;
}
}
dataset-systementity.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<System_Entities id="2" name="NAME" phone01="+52-55-55555555" email="a#a.com"
address01="Street" address02="123" address03="1" address04="Address04"
address05="Address05" city="City" state="State" country="MX"
zipcode="12345" />
</dataset>
Error
ERROR DatabaseDataSet:286 - Table 'System_Entities' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
I can see that the tables are created by hibernate because the log shows all the sql sentences without error.
Thanks.
SOLUTION
Thanks Mark Robinson
I modified the setUp method to:
#Before
public void setUp() throws Exception {
IDatabaseConnection dbUnitCon = null;
EntityManager entityManager = entityManagerFactory.createEntityManager();
Session session = entityManager.unwrap(Session.class);
SessionImplementor si = (SessionImplementor) session;
Connection conn = si.getJdbcConnectionAccess().obtainConnection();
dbUnitCon = new DatabaseConnection(conn);
//dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);
}
It works now, what I don't understand yet is if I use HSQLDB I don't have this problem.
The problem is that DBUnit is loading the table data before Hibernate can initialize.
As part of your #setup, you'll need to get the Hibernate session. This should cause Hibernate to create your table. You could even force it by executing a simple query like select 1

Spring Programmatic Jdbc Transaction rollback doesn't work

I use spring transaction to include a few db update operation into a single transaction. let say there 2 db updates within a single transaction. the update 1 is successful while the second fails. my problem is when such a case happens, the first db update get committed to db even though the second db update failed which leads to transaction rollback.
XML declaration:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
<bean id="testDao" class="dao.TestDao">
<constructor-arg >
<ref local="simpleJdbcTemplate" />
</constructor-arg>
<constructor-arg >
<ref local="txManager" />
</constructor-arg>
</bean>
java code:
public class DaoCallback extends TransactionCallbackWithoutResult {
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
try{
dbUpdate1();
dbUpdate2();
}catch(Exception e){
arg0.setRollbackOnly();
}
}
i intentionally make the dbUpdate1 to success and the dbUpdate2 to fail so as the test out whether the rollback really works. When I debug through my code, i can see that the control flow run into the catch exception and the "setRollbackOnly()" method is called.
But when I check the database, I can see the change from dbUpdate1(). So please help explain what is wrong here?
Dara kok,
I've found out the problem. It's not the code i've that cause the problem. it's MySQL data storage configuration. MyISAM doesn't support transaction.
Spring should have shown some kind of error message so that developer can know that a transaction is being called on a database engine without transaction support.

transaction timeout not working on hibernate with oracle

I am having problem setting the transaction timeout for hibernate on oracle. It does not work.Can anyone help?
The "SaveOrUpdate" will not return within the stated 10 seconds. It will hang for a very long time.
I am using Oracle 10r2.
Hibernate Configuration File
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#9.9.9.9:1521:orcl</property>
<property name="connection.username">foouser</property>
<property name="connection.password">foopass</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="foo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate class
public class foo implements Serializable
{
...
public void save() throws Exception
{
Session dbSession = null;
Transaction tran = null;
try
{
dbSession = PersistenceMgr.getPersistenceMgr().getDbSession();
tran = dbSession.beginTransaction();
tran.setTimeout(10); // 10 seconds
dbSession.saveOrUpdate(this);
tran.commit();
}
catch (HibernateException e)
{
if(tran!=null)
{
try{tran.rollback();}
catch(HibernateException he){}
}
...
}
finally
{
if( dbSession != null )
{
try{dbSession.close();}
catch(HibernateException e){}
}
}
}
}
The timeout needs to be set before the transaction is started.
instead of
tran = dbSession.beginTransaction();
tran.setTimeout(10);// 10 seconds
try
tran = dbSession.getTransaction();
tran.setTimeout(10);
tran.begin();
You can see that at: http://community.jboss.org/wiki/TransactionTimeout.
Is this using JTA? If not, JDBC itself has no API for setting a transaction timeout so instead Hibernate attempts to manage that by tracking how much of the specified timeout period remains when executing a JDBC Statement within the transaction and setting the Statement.setQueryTimeout. It could be that there is either a bug in that logic or that the Oracle JDBC driver has a bug with regard to setting statement timeout.
If you find it is a Hibernate bug, https://hibernate.onjira.com

Resources