I'm trying to use Log4j 2 to save the logs generated from a web application into a table in a DB2 10.5 database. As a servlet container i'm using Tomcat 8.
My data source declaration, inside Tomcat's context.xml:
<Resource
type="javax.sql.DataSource"
name="jdbc/NCRDS"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://192.168.1.58:50000/NCR"
username="admin"
password="admin"
initialSize="0"
maxActive="80"
maxIdle = "30"
minIdle="20"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
testOnBorrow="true"
validationQuery="VALUES 1"
validationInterval="30000"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
abandonWhenPercentageFull="60"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer"/>
The appender declared insinde log4j2.xml:
<JDBC name="databaseAppender" tableName="admin.logs">
<DataSource jndiName="java:/comp/env/jdbc/NCRDS" />
<Column name="evendDate" isEventTimestamp="true" />
<Column name="Level" pattern="%level" />
<Column name="Logger" pattern="%logger" />
<Column name="Message" pattern="%msg" />
</JDBC>
The jdbc resource is also declared in my application's web.xml file. While i've managed to insert the logs on a MySQL database (changing of course the required information in my configuration files), i'm not able to achieve the same in DB2. The following stack trace part is what seems to be causing the problem:
Caused by: java.lang.AbstractMethodError: com.ibm.db2.jcc.am.po.setNString(ILjava/lang/String;)V
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.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport$StatementProxy.invoke(AbstractQueryReport.java:233)
at com.sun.proxy.$Proxy40.setNString(Unknown Source)
at org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.writeInternal(JdbcDatabaseManager.java:110)
at org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager.write(AbstractDatabaseManager.java:167)
at org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender.append(AbstractDatabaseAppender.java:105)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:99)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:430)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:409)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:367)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:112)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:738)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:708)
at org.apache.logging.log4j.spi.AbstractLogger.debug(AbstractLogger.java:237)
at com.ikubinfo.fileservice.FileSystemFileManager.<init>(FileSystemFileManager.java:53)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 24 more
I can't seem to find any information regarding this error and i really can explain to myself what's going wrong. I'd be very grateful for your help.
What i'm using: Eclipse Luna, Tomcat 8, DB2 10.5, db2jcc.jar , jdk1.7, log4j required jars.
This: Caused by: java.lang.AbstractMethodError: com.ibm.db2.jcc.am.po.setNString basically says that the DB2 driver does not implement the method setNString(), which Log4j is trying to use at org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.writeInternal(JdbcDatabaseManager.java:110).
Now, what to do about it I can't tell. May be there is a parameter that can instruct Log4j to use setString() instead, or may be, Log4j being open source, you could edit the JdbcDatabaseManager class.
The problem here, as mustaccio correctly stated, was that the setNString() method was called which was trying to set var args, but instead, the log4j2 implementation could offer only a setString() method for a DB2 configuration . This call was caused by the defined column names. My first case:
<Column name="Date" isEventTimestamp="true" />
<Column name="Level" pattern="%level" isUnicode="false"/>
<Column name="Logger" pattern="%logger" isUnicode="false"/>
<Column name="Message" pattern="%msg" isUnicode="false"/>
You can see the names had both upper and lower case chars. Now i've got two solutions:
1. I could have added the HTML entity for the quotation mark, because DB2 needs quotation marks to read lower-case chars. So the final form would have been: <Column name=""Date""
2. The one which i'm following, rename the table names to upper-cases and simply do the same on the column references in log4j2.xml: <Column name="DATE"
Related
I am getting JdbcSQLIntegrityConstraintViolationException exception while starting my server.
Seems like some problem in using <map>, earlier it was working file with older version of H2 but now not working.
My curren H2 version is:
implementation group: 'com.h2database', name: 'h2', version: '1.4.200'
Older was:
compile group: 'com.h2database', name: 'h2', version: '1.4.193'
My class is using simple map variable like: private Map<String, String> extraData;
Can someone tell me, how to fix this???
Thanks
Here is my XML snippet:
<class name="pojo.MachineInstruction" >
<id name="machineId" type="java.lang.Integer"/>
<!--Time stamp is auto generated. No need to set the value..-->
<timestamp name="timestamp"/>
<property name="instructionType" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">pojo.MachineInstructionType</param>
</type>
</property>
<property name="instructionStatus" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">pojo.MachineInstructionStatus</param>
</type>
</property>
<property name="version" type="java.lang.String"/>
<map name="extraData" cascade="all">
<key column="extraData" />
<map-key type="text" column="key"/>
<element type="text" column="value"/>
</map>
</class>
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PRIMARY KEY ON """".PAGE_INDEX"; SQL statement:
ALTER TABLE PUBLIC.MACHINEINSTRUCTION_EXTRADATA ADD CONSTRAINT PUBLIC.FKROHO504EJPG9G4R81YYKTV44K FOREIGN KEY(EXTRADATA) REFERENCES PUBLIC.MACHINEINSTRUCTION(MACHINEID) NOCHECK [23505-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:459)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.pagestore.db.PageDataIndex.add(PageDataIndex.java:125)
at org.h2.pagestore.PageStore.addMeta(PageStore.java:1804)
at org.h2.pagestore.db.PageBtreeIndex.<init>(PageBtreeIndex.java:65)
at org.h2.pagestore.db.PageStoreTable.addIndex(PageStoreTable.java:183)
at org.h2.command.ddl.AlterTableAddConstraint.createIndex(AlterTableAddConstraint.java:298)
at org.h2.command.ddl.AlterTableAddConstraint.tryUpdate(AlterTableAddConstraint.java:223)
at org.h2.command.ddl.AlterTableAddConstraint.update(AlterTableAddConstraint.java:78)
at org.h2.engine.MetaRecord.execute(MetaRecord.java:60)
at org.h2.engine.Database.open(Database.java:759)
at org.h2.engine.Database.openDatabase(Database.java:307)
at org.h2.engine.Database.<init>(Database.java:301)
at org.h2.engine.Engine.openSession(Engine.java:74)
at org.h2.engine.Engine.openSession(Engine.java:192)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:171)
at org.h2.engine.Engine.createSession(Engine.java:166)
at org.h2.engine.Engine.createSession(Engine.java:29)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:340)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:173)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:152)
at org.h2.Driver.connect(Driver.java:69)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
... 35 more
Unfortunately, H2 1.4.* and older versions don't have automatic upgrade procedure, it is under responsibility of their users.
https://h2database.com/html/tutorial.html#upgrade_backup_restore
(The upcoming H2 2.0 will have a built-in upgrade utility and will refuse to open old files without upgrade to avoid such issues.)
1.4.196 and older versions had a bug with some combinations of indexes and referential constraints, it was fixed in 1.4.197, but upgrade path for the legacy PageStore engine wasn't provided. MVStore also had similar upgrade issues, but they were fixed.
Right now your database seems to be corrupted, because upgrade wasn't performed. You need to write some dummy implementation of org.h2.api.DatabaseEventListener, put it into the classpath of your application (classpath of your server if you use separate H2 Server process), and open your database with ;DATABASE_EVENT_LISTENER='path.to.YourListener' appended to JDBC connection URL. Wrong constraints will be logged to your listener. After that you will be able to export your database to the SQL Script with SCRIPT TO 'filename.sql' command and create a new database file and populate it with that script with RUNSCRIPT FROM 'filename.sql'.
I was wondering if anyone has any code examples for setting up a connection pool in Tomcat (7 or later) using MyBatis as the ORM.
I presume I need to add a resource to my context.xml file in my Tomcat conf folder and then link that to MyBatis. I've had a look and any tutorials I have found seem to be Spring specific. Does anyone have a simple tutorial or can they outline the steps required to get this up and running?
There is this old entry in the iBatis FAQ that should still be applicable to myBatis: How do I use a JNDI DataSource with iBATIS in Tomcat?.
You can google for the details to configure a datasource in your Tomcat version, then configure MyBatis as well (it's different than the iBatis configuration):
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/yourDatasourceName"/>
</dataSource>
</environment>
There are a number of ways you can do this. I am assuming that you mean to implement Tomcat (as opposed to Spring) managed connection pooling. I have tested on MySQl/Spring 4/Mybatis-spring 1.1 stack.
Figure out the connection pooling mechanism you want to implement (C3P0/DBCP etc).
If you want to use your database as a JNDI data source, then you have to declare it as a resource in Tomcat settings. There are a number of ways to do this. You can follow this well written guide Tomcat JNDI connection pooling. I generally add the following entry in context.xml located in the META-INF directory of my app:
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
This file is used by Tomcat to invoke its own default connection pool, which in this case would be DBCP 2. You can tweak its settings by adding more parameters (like removeAbandonedOnBorrow=true) in the Resource element above. You should put the driver jar (eg MySQL Connector Jar) of your database in Tomcat's lib folder.
After this, depending on your Mybatis implementation (XML or bean based), you can inject this data source into Mybatis. If you are doing it the bean way, you can do it like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/TestDB"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dbDataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbDataSource" />
<property name="typeAliasesPackage" value="com.example.model"/>
<property name="mapperLocations" value="classpath*:*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
MapperScannerConfigurer is necessary because it searches the directories given to it to find mapper xml files. You can also use the old way of defining a mybatis-config.xml file by injecting the file's location in the sqlSessionFactory bean in its configLocation property.
I am using Solr 4.6.1 on Mac. I try to figure out how to use data import to load data from MySQL. But I end up with fail to load JDBC Driver class. Here is what I did:
Put mysql-connector-java-5.1.29-bin.jar to ~/Tools/Solr/example/lib/
1.Configure ~/Tools/Solr/example/solr/collection1/conf/data-config.xml:
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
user="root"
password="root"/>
<document>
<entity name="SolrTest"
query="select * from SolrTest">
<field column="ID" name="id"/>
<field column="Name" name="name"/>
<field column="Class" name="class"/>
<field column="Score" name="score"/>
</entity>
</document>
</dataConfig>
2.Configure ~/Tools/Solr/example/solr/collection1/conf/solrconfig.xml:
add the following under < config> < /config>
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
also
<lib dir="../../../dist/" regex="solr-dataimporthandler-\d.*\.jar" />
<lib dir="../../../contrib/dataimporthandler/lib" regex=".*\.jar" />
3.Add the following to ~/Tools/Solr/example/solr/collection1/conf/schema.xml:
<field name="id" type="int" indexed="true" stored="true" required="true"/>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="class" type="string" indexed="true" stored="true" />
<field name="score" type="int" indexed="true" stored="true"/>
Then run
java -jar start.jar
When I go to
http://localhost:8983/solr/#/collection1/dataimport//dataimport
it shows
No information available (idle)
When I click the execute button, I got the following error message:
31537 [Thread-15] ERROR org.apache.solr.handler.dataimport.DataImporter – Full Import
failed:java.lang.RuntimeException: java.lang.RuntimeException:
org.apache.solr.handler.dataimport.DataImportHandlerException: Could
not load driver: com.mysql.jdbc.Driver Processing Document # 1 at
org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:270)
at
org.apache.solr.handler.dataimport.DataImporter.doFullImport(DataImporter.java:411)
at
org.apache.solr.handler.dataimport.DataImporter.runCmd(DataImporter.java:476)
at
org.apache.solr.handler.dataimport.DataImporter$1.run(DataImporter.java:457)
Caused by: java.lang.RuntimeException:
org.apache.solr.handler.dataimport.DataImportHandlerException: Could
not load driver: com.mysql.jdbc.Driver Processing Document # 1 at
org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:410)
at
org.apache.solr.handler.dataimport.DocBuilder.doFullDump(DocBuilder.java:323)
at
org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:231)
... 3 more Caused by:
org.apache.solr.handler.dataimport.DataImportHandlerException: Could
not load driver: com.mysql.jdbc.Driver Processing Document # 1 at
org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71)
at
org.apache.solr.handler.dataimport.JdbcDataSource.createConnectionFactory(JdbcDataSource.java:116)
at
org.apache.solr.handler.dataimport.JdbcDataSource.init(JdbcDataSource.java:64)
at
org.apache.solr.handler.dataimport.DataImporter.getDataSourceInstance(DataImporter.java:383)
at
org.apache.solr.handler.dataimport.ContextImpl.getDataSource(ContextImpl.java:99)
at
org.apache.solr.handler.dataimport.SqlEntityProcessor.init(SqlEntityProcessor.java:53)
at
org.apache.solr.handler.dataimport.EntityProcessorWrapper.init(EntityProcessorWrapper.java:74)
at
org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:427)
at
org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:408)
... 5 more Caused by: java.lang.ClassNotFoundException: Unable to
load com.mysql.jdbc.Driver or
org.apache.solr.handler.dataimport.com.mysql.jdbc.Driver at
org.apache.solr.handler.dataimport.DocBuilder.loadClass(DocBuilder.java:916)
at
org.apache.solr.handler.dataimport.JdbcDataSource.createConnectionFactory(JdbcDataSource.java:114)
... 12 more Caused by: org.apache.solr.common.SolrException: Error
loading class 'com.mysql.jdbc.Driver' at
org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:470)
at
org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:401)
at
org.apache.solr.handler.dataimport.DocBuilder.loadClass(DocBuilder.java:906)
... 13 more Caused by: java.lang.ClassNotFoundException:
com.mysql.jdbc.Driver at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:789) at
java.lang.ClassLoader.loadClass(ClassLoader.java:356) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:454)
... 15 more
How to solve this?
Create a folder inside your solr installation directory. (say solr-4.6.1/lib)
Place the mysql-connector-java-5.1.29-bin.jar inside the folder.
Edit solrconfig.xml and put the jar's path
<lib dir="../../../lib/" regex="mysql-connector-java-5.1.29-bin.jar" />
Restart Solr.
Also please check your URL. It's having //
http://localhost:8983/solr/#/collection1/dataimport//dataimport
So for those unfortunate souls who end up here. I'm using the latest Solr Docker image (8.4) which is configured so that the solr Unix-user doesn't have any permissions to create/delete folders and whatnot. For some reason I managed to create a lib file which I took for being a directory. After debugging a while I noticed this error, and promptly just changed the
<lib dir="${solr.install.dir:../../../..}/contrib/dataimporthandler/lib" regex=".*\.jar" />
to
<lib dir="${solr.install.dir:../../../..}/contrib/dataimporthandler/" regex=".*\.jar" />
where my postgresql-42.2.10.jar driver is. Otherwise all the other steps apply (edit solrconfig.xml, add data-config.xml, add schema.xml, copy posgresql-*.jar to the container's /opt/solr-8.4.1/contrib/dataimporthandler). Maybe one thing worth of mentioning is that I used Docker's internal network URL instead of localhost: url="jdbc:postgresql://host.docker.internal:5600/my_postgres_db" but I don't think it matters.
And oh, remember to restart Solr after adding the files / making changes =).
In my User.hbm.xml I have:
<property name="delete" type="java.lang.String">
<column name="`delete`" />
</property>
I've also tried with '' and [] but all give the same exception:
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"delete"
If I change the column name from delete to deletea then it works so I know that it's not escaping the column name properly.
You could try setting this property to automatically quote all identifiers:
hibernate.globally_quoted_identifiers=true
Just try changing this
<column name="`name`" />
to this
<column name="name" />
I have been learning about how to set up Tomcat's connection pool through this website. So I made a context.xml file and put it in the directory META-INF. This is what I have right now.
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/gmustudent" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
username="root" password="root"
url="jdbc:mysql://localhost:3306/official"
maxActive="100" maxIdle="10" minIdle="5" initialSize="5" maxWait="10000" />
</Context>
However I would like to specify the class name of the factory. But everytime I add this attribute
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
I get a huge pile of errors in my console. Here are some of them in no particular order.
WARNING: Failed to register in JMX: javax.naming.NamingException: com.mysql.jdbc.Driver
WARNING: Unexpected exception resolving reference java.sql.SQLException: com.mysql.jdbc.Driver
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gmustudent' did not find a matching property.
So when I do not specify a factory the site works just fine. But when I do errors are thrown and nothing works. I did some research on stack overflow and found this post. Because of this I changed my tomcat version from 7.0.11 to the most recent but am still getting the error. So then I thought maybe it is some kind of clash with my factory in my server.xml file but I am not nearly experienced to make that call. But here is the resource in my server.xml file
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
So is this clashing with the factory in my context.xml file? Or am I totally wrong here? In a nut shell I would like to find out how to specify a factory in my context.xml file without getting a huge error. Thank you for reading.
You can actually manage the entire connection in your web app using Spring if you want. Here is an example using PostgreSQL:
<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/mydb"/>
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
</bean>
</beans>
You can just put that in WEB-INF/classes and load it using:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/mycontext.xml");
I wouldn't even bother with having Tomcat manage it at this point.