Spring JDBC No suitable driver found for jdbc:jtds:sqlserver - swing

Hi I am trying to connect SQL Server with JDBC JTDS but it turn out an error: No suitable driver found for jdbc:jtds:sqlserver, I already put it in my dependency and it appear on the Maven library, this is my configure for datasource:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://localhost/mkyongjava;instance=SQLEXPRESS" />
<property name="username" value="sa" />
<property name="password" value="admin123" />
</bean>
I already put jtds driver on Tomcat Server but no thing change
This is my project stuture

You are still providing the MySQL driver class name in the driverClassName property - try changing that to:
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />

Related

Getting The application must supply JDBC connections exception

I am getting Application must provide JDBC connection exception while trying to perform query on Mysql DB using Spring+hibernate
Application context has following entry
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="appDataSource" />
</bean>
<bean id="appDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<property name="url" value="jdbc:mariadb://localhost:3306/appdb" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
And JNDI name as
<jee:jndi-lookup id="appDataSource" jndi-name="java:comp/env/jdbc/Modeling"/>
Complete stack trace
java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:84)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java: )
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1927)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1896)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1874)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2610)
at org.hibernate.loader.Loader.doList(Loader.java:2593)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
at org.hibernate.loader.Loader.list(Loader.java:2417)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:964)
Hibernate configuration
<hibernate-configuration>
<session-factory>
<mapping resource="service.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Service.hbm.xml file
<hibernate-mapping>
<class name="service.repository.Account"
table="ACCOUNTS" discriminator-value="null">
<id name="id" column="id" type="java.lang.Long">
//all other entry
</class>
</hibernate-mapping>
Do the below -
1)check hibernate.cgf.xml(or your hibernate config file) & confirm if below are correct -
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.dialect
hibernate.connection.driver_class
2) Check for the datasource name you have define in your standalone.xml(JBoss config file).It should match with your jndi-name ="java:datasource_name"
I assume your "java:comp/env/jdbc/Modeling" JNDI name does not point to a valid data source object.
Issue is solved after removing Jrebel plugin from server settings.
Some how jrebel was messing with the configuration.
Upgraded rebel configuration and things are working fine now.
If not resolved yet them try giving path of hibernate.cfg.xml to configure object
cfg.configure("hibernate.cfg.xml");
and also can skip dialect property in xml if you are not able to select right one for yourself .
it worked for me.

Adding c3p0 to spring hibernate project

I had some trouble with connection timeouts. Problem: After mysql timeout hibernate stops working. It seams to work fine, but it does not write further data to database. After searching the logs, I found the hint: last connection 53000 seconds - connection problem. Timeout is mysql-default (28000). So I tried increasing mysql timeout, but won't use in production system.
Searching for a better solution, I found out, that probably c3p0 could solve my problem with a test query.
I am using hibernate with spring. That's were trouble starts. I added c3p0 as DataSource (Bean):
<bean id="dataSource_archiv" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="minPoolSize" value="${hibernate.connection.minPoolSize}" />
<property name="maxPoolSize" value="${hibernate.connection.maxPoolSize}" />
<property name="maxIdleTime" value="${hibernate.connection.maxIdleTime}" />
<property name="maxStatements" value="${hibernate.connection.maxStmts}" />
<property name="testConnectionOnCheckout" value="${hibernate.connection.testCo}" />
<property name="testConnectionOnCheckin" value="${hibernate.connection.testCo}" />
<property name="preferredTestQuery" value="${hibernate.connection.testSQL}" />
<property name="idleConnectionTestPeriod" value="${hibernate.connection.testPeriod}" />
</bean>
Complete hibernate Config File.
But afterwards, the application cannot be started anymore:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field
Complete Stack trace.
So probably I misconfigured something. Where can I start analyzing the problem?
Or is there any other good solution? Like starting a Native SQL from hibernate to keep connection alive? I would prefer the solution with c3p0.
Thanks.

init in MySQL url for Hibernate datasource

I am trying to create the schema name with my mysql (local database) url but its not working. Its working with H2 (in memory database) URL tho. Please advice.
H2 configuration -
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url"
value="jdbc:h2:mem:test;DB_CLOSE_DELAY=10;INIT=CREATE SCHEMA IF NOT EXISTS products" />
</bean>
MySQL configuration - (Tried various combination of init to the below but will not work)
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
Thanks

spring + hibernate + data base connection pooling and high availability

I'm trying to configure my web application like this:
1 tomcat server
2 mysql data bases in master/ master replication for failover purpose.
I also succeded to set up a pooled connexion using c3p0, thus avoiding to open a new connexion for each request(when no failover is involved)
I would like to use the failover support porvided by the jdbc connector in order to route request to the second database when the first one is unavailabe. Thus i'm using the fail over capability of the jdbc driver, simply by providing extra configuration parameters.
For the moment, when one server is unvailable, the requst are routed to the second available server. However, i'm facing a problem with a new opened connexion on the database for each request.
It seems that combaining connexion pooling and jdbc failover dosen't work toogther ?
Please advice, also regarding configuration parameters
<bean id="mydataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306,myOtherServer:3306/zeus?autoCommit=true&autoReconnect=true&autoReconnectForPools=true&failOverReadOnly=false"/>
<property name="user" value="root" />
<property name="password" value="root" />
<!-- these are C3P0 properties -->
<!-- property name="acquireIncrement" value="${acquireIncrement}" /_-->
<property name="minPoolSize" value="6" />
<property name="maxPoolSize" value="10" />
<property name="maxIdleTime" value="100" />
</bean>

Database Connections Aren't Closing

I'm using spring 3.0 (jdbcTemplate), Tomcat, MySQL and C3p0 to handle my database activities. I'm using both jdbctemplate and simplejdbctemplate which will take care of creating and closing connections, statements, resultsets etc. I'm using C3p0 for connection pooling however the connections are remaining open and eventually the app will run out of connections.
Here is the configuration of my data source:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="5"></property>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="5"/>
<property name="maxIdleTime" value="30"/>
<property name="maxIdleTimeExcessConnections" value="30"/>
<property name="maxConnectionAge" value="30"/>
<property name="checkoutTimeout" value="100"/>
<property name="maxStatements" value="50"></property>
<property name="automaticTestTable" value="C3P0_TEST_TABLE"></property>
<property name="testConnectionOnCheckin" value="true"></property>
<property name="idleConnectionTestPeriod" value="30"></property>
</bean>
I'm also using TransactionManagement provided by spring - here is the configuration of that:
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
Here is the rest of the datasource configuration:
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>
<bean id="userDAO" class="com.Test.dao.UserDAOImpl">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
</bean>
Finally here is a method where I update records into a database:
#Transactional(readOnly=false)
public void updateBenchMarkCumulative(List<BenchMarkCumulative> bmCumulativeList)
{
List<Object[]> parameters = new ArrayList<Object[]>();
for(BenchMarkCumulative bmCumulative : bmCumulativeList)
{
parameters.add(new Object[]{bmCumulative.getCumulativeAmt(), bmCumulative.getPkBenchMarkCumulative()});
}
this.simpleJdbcTemplate.batchUpdate(UPDATE_BENCHMARK_CUMULATIVE, parameters);
}
Is there something I'm doing wrong in my configuration or am I missing something that needs to be added to the configuration or coding?
Here is the exception being thrown:
INFO [http-8080-1] (AbstractPoolBackedDataSource.java510) - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ]
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
Thank you in advance.
Keith
I would agree with axtavt. I was in a similar situation, and there were couple places I was manually opening connections but not closing them (stored procedure calls for iSeries). Please verify if you have that kind of spots.