-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Tomcat MySQL Integration
PostPosted: Tue Mar 09, 2004 9:33 pm 
Newbie

Joined: Tue Mar 09, 2004 8:52 pm
Posts: 5
I am trying to configure a Tomcat datasource for use with MySQL using the hilo approach. I see numerous post regarding the autocommit error, but not sure if/when it applies. Is it not possible to use hilo with a tomcat datasource and mysql? If so, how and what should I change? If not, what is the best alternative?

This example works if I change to the identify generator. However I would rather avoid the additional SQL calls.
It also works if I remove the datasource settings in tomcat server.xml, web.xml and configure hibernate.cfg.xml to remove the datasource and use:

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/academy</property>
<property name="hibernate.connection.username">learning</property>
<property name="hibernate.connection.password">academy</property>

After reading through various posting and supplied documentation, it appears this is not the proper configuration for production systems.

Please point me in the right direction. The documentation far exceeds what I am currently using (OJB) and code looks clean. I just get the datasource to work properly with tomcat.

Thanks for the help and sorry for the long post.
Rafe


public void init() throws ServletException {
Config.getConfig().setResources(super.getResources());

try {
System.out.println("Got into init");
Session session = HibernateUtil.currentSession();
System.out.println("Got here");
Transaction tx= session.beginTransaction();
User user = new User();
user.setFirstName("Ed");
user.setLastName("King");
user.setLogonName("eking");
user.setPassword("test");
user.setQuestion("What is my favorite state?");
user.setAnswer("texas");
user.setEmailAddress("eking_mt@hotmail.com");
user.setIsAccountDisabled(false);
session.save(user);
tx.commit();

HibernateUtil.closeSession();
}
catch (Exception e) {
System.out.println("Got into Error");
e.printStackTrace();

}
}

---------------- tomcat/conf/server.xml---------------------------
<Context path="/agworks" docBase="agworks" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/agworks" auth="Container" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/agworks">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- DBCP database connection settings -->
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/academy?autoReconnect=true</value>
</parameter>

<parameter>
<name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>learning</value>
</parameter>
<parameter>
<name>password</name>
<value>academy</value>
</parameter>

<!-- DBCP connection pooling options -->
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->

<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->

<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->

<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
</ResourceParams>
</Context>
---------------------------------------------------------

----------------------tomcat web.xml------------------
<!-- rsh configuration of connection pool -->
<description>MySQL Connection Pool </description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/agworks</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
--------------------------------------------------------
----------------------hibernate.cfg.xml------------------------
<hibernate-configuration>

<session-factory>

<property name="show_sql">true</property>
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name="connection.datasource">java:comp/env/jdbc/agworks</property>

<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="use_outer_join">true</property>

<!-- Mapping files -->
<mapping resource="user.hbm.xml"/>

</session-factory>
--------------------------------------------------------------

-----------------------Error Message When Saving Object----------------------
Mar 9, 2004 6:28:10 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on port 8080
Mar 9, 2004 6:28:10 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1078 ms
Mar 9, 2004 6:28:10 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Mar 9, 2004 6:28:10 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.0.16
Mar 9, 2004 6:28:10 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Mar 9, 2004 6:28:10 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Tomcat5.0\webapps\agworks\WEB-INF\lib\j2ee.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Got into init
18:28:24,015 INFO Environment:462 - Hibernate 2.1.2
18:28:24,093 INFO Environment:491 - hibernate.properties not found
18:28:24,125 INFO Environment:519 - using CGLIB reflection optimizer
18:28:24,218 INFO Configuration:854 - configuring from resource: /hibernate.cfg.xml
18:28:24,234 INFO Configuration:826 - Configuration resource: /hibernate.cfg.xml
18:28:25,437 INFO Configuration:311 - Mapping resource: user.hbm.xml
18:28:26,984 INFO Binder:229 - Mapping class: com.agworks.model.User -> user
18:28:28,453 INFO Configuration:1017 - Configured SessionFactory: null
18:28:28,484 INFO Configuration:595 - processing one-to-many association mappings
18:28:28,500 INFO Configuration:604 - processing one-to-one association property references
18:28:28,515 INFO Configuration:629 - processing foreign key constraints
18:28:28,843 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
18:28:28,859 INFO SettingsFactory:62 - Use outer join fetching: true
18:28:28,968 INFO NamingHelper:26 - JNDI InitialContext properties:{}
18:28:29,218 INFO DatasourceConnectionProvider:51 - Using datasource: java:comp/env/jdbc/agworks
18:28:29,265 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
18:28:29,343 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
18:28:30,625 INFO SettingsFactory:102 - Use scrollable result sets: true
18:28:30,656 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): true
18:28:30,671 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
18:28:30,687 INFO SettingsFactory:114 - echoing all SQL to stdout
18:28:30,703 INFO SettingsFactory:117 - Query language substitutions: {}
18:28:30,718 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
18:28:30,812 INFO Configuration:1080 - instantiating and configuring caches
18:28:32,093 INFO SessionFactoryImpl:119 - building session factory
18:29:28,828 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
Got here
18:30:11,265 WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null
18:30:11,265 ERROR JDBCExceptionReporter:46 - Can't call commit when autocommit=true
18:30:11,296 ERROR JDBCExceptionReporter:38 - Could not save object
java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.Connection.commit(Connection.java:1073)
at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:248)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:244)
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:126)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:747)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
at com.agworks.servlets.AgworksStartupServlet.init(AgworksStartupServlet.java:55)
at javax.servlet.GenericServlet.init(GenericServlet.java:256)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1044)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:887)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3948)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4271)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1125)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:816)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1125)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:518)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2343)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:297)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:398)
Got into Error


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 10:34 pm 
Newbie

Joined: Tue Mar 09, 2004 8:52 pm
Posts: 5
i was able to get it to work by changing the connection in tomcat's server.xml file. So I guess you can utilize a tomcat defined connection pool with mysql. The changed line is as follows:

<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/academy?relaxAutoCommit=true</value>

i would like to set autoReconnect and relaxAutoCommit with the following but get a compile error:
<value>jdbc:mysql://localhost:3306/academy?autoReconnect=true&relaxAutoCommit=true</value>

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 8:08 pm 
Newbie

Joined: Mon Nov 17, 2003 3:50 pm
Posts: 19
Try this:

<value>jdbc:mysql://localhost:3306/academy?autoReconnect=true&amp;relaxAutoCommit=true</value>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.