As this is a part of the hibernate configuration rather than the
database / datasource configuration the SQL dialect has to be configured
in the hibernate configuration file, not in the context.
Here's my SessionFactory class just in case:
(I think this could be realized in a better way using Spring / DI but
nonetheless should help for the start)
N.B.: I found this on the net, it is not my own work - just don't
remember where I found it)
package de.pma.dbobjects.oasis;
import de.pma.dbobjects.oasis.oasis.*;
import de.pma.utils.Deployment;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* This class garanties that only one single SessionFactory
* is instanciated and that the configuration is done thread
safe as
* singleton. Actually it only wraps the Hibernate SessionFactory.
* When a JNDI name is configured the session is bound to to JNDI,
* else it is only saved locally.
* You are free to use any kind of JTA or Thread
transactionFactories.
*/
public class OasisSessionFactory {
private OasisSessionFactory() {
throw new AssertionError("must not be instanciated");
}
/**
* Location of hibernate.cfg.xml file. NOTICE: Location should be
on the
* classpath as Hibernate uses #resourceAsStream style lookup for its
* configuration file. That is place the config file in a Java
package - the
* default location is the default Java package.<br>
* <br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** The single instance of hibernate configuration */
private static final AnnotationConfiguration cfg = new
AnnotationConfiguration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* initialises the configuration if not yet done and returns the
current
* instance
*
* @return
*/
public static SessionFactory getInstance() {
if (sessionFactory == null) {
initSessionFactory();
}
return sessionFactory;
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public Session openSession() {
return sessionFactory.getCurrentSession();
}
/**
* The behaviour of this method depends on the session context you have
* configured. This factory is intended to be used with a
hibernate.cfg.xml
* including the following property <property
* name="current_session_context_class">thread</property> This
would return
* the current open session or if this does not exist, will create
a new
* session
*
* @return
*/
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
/**
* initializes the sessionfactory in a safe way even if more than
one thread
* tries to build a sessionFactory
*/
private static synchronized void initSessionFactory() {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
// Die Klassen hinzufuegen
cfg.addAnnotatedClass(blah.class);
//add your annotated classes here
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
throw new HibernateException("Could not initialize the
Hibernate configuration", e);
}
}
}
public static void close() {
if (sessionFactory != null) {
sessionFactory.close();
}
sessionFactory = null;
}
}
dishmily schrieb:
is it possible to define SQL Dialects "org.hibernate.dialect.MySQLDialect" in
context.xml in Tomcat? how? thanks.
Jens Greven wrote:
You can also use c3p0 with Tomcat JNDI, e. g.
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/SupportPortal">
<Resource name="jdbc/MYSQL"
auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
description="DB Connection"
jdbcUrl="jdbc:mysql://server:3306/mysql?autoReconnect=true"
driverClass="com.mysql.jdbc.Driver"
user="root"
password="password"
maxPoolSize="20"
minPoolSize="1"
initialPoolSize="1"
acquireIncrement="1"
maxConnectionAge="0"
maxIdleTime="1800"
maxIdleTimeExcessConnections="120"
idleConnectionTestPeriod="30"
testConnectionOnCheckout="true"
connectionCustomizerClassName="de.pma.dbobjects.DefaultConnectionCustomizer"
factory="org.apache.naming.factory.BeanFactory"
/>
</Context>
and a hibernate config file like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property
name="hibernate.connection.datasource">java:comp/env/jdbc/MYSQL</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">false</property>
<property
name="hibernate.current_session_context_class">thread</property>
<property
name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
</session-factory>
</hibernate-configuration>
dishmily schrieb:
thank you, it works!
there are two factor need to mention:
1) as i used hibernate to create Database-connection before, the
connection
pool C3P0 was used in hibernate. but when i try to maintain
Database-connection through JNDI in tomcat, i donn't need C3P0 anymore.
so,
i SHOULD delete configuration about C3P0 in hibernate configuration file.
The error message i got had something to do with it.
2) I SHOULD add a ResourceLink in the Context.xml, in order to let
Hibernate
using the global resource, witch is in read only mode.
(https://forums.hibernate.org/viewtopic.php?f=1&t=937920&start=0)
<ResourceLink name="jdbc/mysql" global="jdbc/mysql"
type="javax.sql.DataSource"/>
Pid Ster wrote:
On 23/11/2009 16:03, dishmily wrote:
i have made few changes, but it didn't work.
(my project uses tomcat and hibernate.)
You didn't mention that before.
You need to configure Hibernate to use the DataSource "jdbc/mysql" that
you've created. It doesn't appear to be correctly configured at the
moment.
You can test that the db connection pool is working:
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
1) $Tomcat_Home\conf\context.xml was changed to:
This is the default context definition, rather than a web app specific
one. Just so you know.
To configure it for a specific web app, include a context.xml definition
in the META-INF folder of your webapp.
p
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/mysql" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="db" password="pwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db"/>
</Context>
2)<resource-ref> was added into
$Tomcat_Home\webapps\axis2\WEB-INF\web.xml
:
<web-app>
...
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
....
</web-app>
wenn i ran the web application, i got the error message:
INFO: Initializing c3p0 pool...
com.mchange.v2.c3p0.poolbackeddatasou...@29fb0733 [
connectionPoolDataSource
-> com.mchange.v2.c3p0.wrapperconnectionpooldatasou...@dd3d444b [
acquireIncrement -> 5, acquireRetryAttempts -> 30, acquireRetryDelay
->
1000, autoCommitOnClose -> false, automaticTestTable -> null,
breakAfterAcquireFailure -> false, checkoutTimeout -> 0,
connectionCustomizerClassName -> null, connectionTesterClassName ->
com.mchange.v2.c3p0.impl.DefaultConnectionTester,
debugUnreturnedConnectionStackTraces -> false, factoryClassLocation ->
null,
forceIgnoreUnresolvedTransactions -> false, identityToken ->
1hgeigx859d0cr2vfrvc8|1ba92db, idleConnectionTestPeriod -> 7200,
initialPoolSize -> 10, maxAdministrativeTaskTime -> 0,
maxConnectionAge
->
0, maxIdleTime -> 14400, maxIdleTimeExcessConnections -> 0,
maxPoolSize
->
100, maxStatements -> 100, maxStatementsPerConnection -> 0,
minPoolSize
->
10, nestedDataSource ->
com.mchange.v2.c3p0.drivermanagerdatasou...@b33bd229
[ description -> null, driverClass -> null, factoryClassLocation ->
null,
identityToken -> 1hgeigx859d0cr2vfrvc8|87ad67, jdbcUrl -> null,
properties
-> {useUnicode=true, autocommit=false, characterEncoding=UTF8} ],
preferredTestQuery -> null, propertyCycle -> 0,
testConnectionOnCheckin
->
false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout
->
0,
usesTraditionalReflectiveProxies -> false; userOverrides: {} ],
dataSourceName -> null, factoryClassLocation -> null, identityToken
->
1hgeigx859d0cr2vfrvc8|15b55bc, numHelperThreads -> 3 ]
23.11.2009 16:51:25
com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNUNG:
com.mchange.v2.async.threadpoolasynchronousrunner$deadlockdetec...@170ec24
-- APPARENT DEADLOCK!!! Creating emergency threads for unassigned
pending
tasks!
23.11.2009 16:51:25
com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNUNG:
com.mchange.v2.async.threadpoolasynchronousrunner$deadlockdetec...@170ec24
-- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@c5d9c1
(com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@15b4ad2
(com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@8d3d62
(com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks:
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@b9132a
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@996b65
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@59c8b5
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@881cb3
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@143753
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@13c4c09
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@1a40247
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
23.11.2009 16:51:34
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
WARNUNG:
com.mchange.v2.resourcepool.basicresourcepool$acquiret...@c5d9c1
--
Acquisition Attempt Failed!!! Clearing pending acquires. While trying
to
acquire a needed new resource, we failed to succeed more than the
maximum
number of allowed acquisition attempts (30). Last acquisition attempt
exception:
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at
com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)
at
com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)
at
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)
at
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)
at
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at
com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at
com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Pid Ster wrote:
On 23/11/2009 13:00, dishmily wrote:
i use 3 tomcats in one PC, in each tomcat i have a webservice, for
each
webservice i use a mysql database.
my question is:
how can i write a config file in each tomcat to let tomcat1 load DB1,
tomcat2 load DB2 and tomcat3 load DB3.
thanks.
I'm guessing that you're using Tomcat 6.0, because you didn't say.
The extensive documentation is often a good place to start:
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
p
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org