问题
I have tried C3po library for connection pooling but I am gettting errors such as abstractMethodError()
for my code I then decided to go with the tomcat pooling thing. Now I am getting an
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1062)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:522)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at classes.session.SessionCreate.buildSessionFactory1(SessionCreate.java:24)
at classes.session.SessionCreate.<clinit>(SessionCreate.java:9)
at classes.verifydata.CheckUniqueUserPass.uniqueUserPass(CheckUniqueUserPass.java:16)
at org.apache.jsp.DataGen.CheckUserPassAvailable_jsp._jspService(CheckUserPassAvailable_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
10:56:19,762 INFO [http-bio-8081-exec-5] StatisticalLoggingSessionEventListener:275 - Session Metrics {
42607 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
Jan 06, 2015 10:56:19 AM org.apache.catalina.core.StandardWrapperValve invoke
I have the Hibernate configuration file as such
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/secure_pass</property>
<property name="hibernate.connection.url">connectionurl</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="net.sf.jasperreports.hql.query.run.type">list</property>
<property name="net.sf.jasperreports.jdbc.fetch.size">50</property>
<property name="net.sf.jasperreports.hql.clear.cache">true</property>
<property name="net.sf.jasperreports.hql.query.list.page.size">50</property>
......
</session-factory>
</hibernate-configuration>
I also have the context-xml file as such :
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/DVDStore">
<Resource
auth="Container"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
maxActive="30" maxIdle="10"
maxWait="10000"
name="jdbc/secure_pass"
password=""
type="javax.sql.DataSource"
url="_DEV;"
username="r"/>
</Context>
Do not worry about values here... Now I am getting the above exception: Please help. I have done All that which is required as per this link
回答1:
I had to debug a similar issue, possible case is you have hibernate.properties somewhere in your class path, may be in one of the third party jars.
In my case developer had added supporting test jar accompanied with third party jar which had hibernate.properties.
Culprit is populating property hibernate.connection.username or hibernate.connection.password
If these properties get populated with non null values, hibernate will call
@Override
public Connection getConnection() throws SQLException {
if ( !available ) {
throw new HibernateException( "Provider is closed!" );
}
return useCredentials ? dataSource.getConnection( user, pass ) : dataSource.getConnection();
}
dataSource.getConnection( user, pass )
useCredentials being true
This method in class org.apache.commons.dbcp.BasicDataSource is as follows
public Connection getConnection(String user, String pass) throws SQLException {
// This method isn't supported by the PoolingDataSource returned by
// the createDataSource
throw new UnsupportedOperationException("Not supported by BasicDataSource");
// return createDataSource().getConnection(username, password);
}
Property useCredentials is populated as
user = (String) configValues.get( Environment.USER );
pass = (String) configValues.get( Environment.PASS );
useCredentials = user != null || pass != null;
In method
public void configure(Map configValues)
回答2:
Spring will call the getConnection(String username, String password) method to establish connection with database. But default getConnection(String username, String password) method from dbcp .jar throws UnsupportedOperationException. So you need to override that method as follows:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import org.apache.commons.dbcp.BasicDataSource;
public class MyDataSource extends BasicDataSource {
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return createDataSource().getConnection();
}
}
来源:https://stackoverflow.com/questions/27792692/tomcat-connection-pooling-java-lang-unsupportedoperationexception-not-support