PreparedStatement Logging - log4j

I use log4j for the logging.
I could see the SQLs through Log4j like below.
Here's my java source which access data base with jdbcTemplate.
public QnaDTO selectBoard(int articleID) {
String SQL =
"SELECT " +
" QA.ARTICLE_ID, " +
" QA.EMAIL, " +
" QA.TEL, " +
" QA.CATEGORY_ID, " +
" CG.CATEGORY_NAME, " +
" QA.SUBJECT, " +
" QA.CONTESTS, " +
" QA.WRITER_NAME, " +
" QA.WRITER_ID, " +
" QA.READCOUNT, " +
" QA.ANSWER, " +
" QA.FILE_NAME, " +
" QA.OPEN_FLG, " +
" QA.KTOPEN_FLG, " +
" TO_CHAR(QA.WRITE_DAY, 'YYYY.MM.DD') WRITE_DAY, " +
" QA.DISPOSAL_FLG " +
"FROM QNA QA JOIN QNA_CATEGORY_GROUP CG " +
"ON QA.CATEGORY_ID = CG.CATEGORY_ID " +
"WHERE QA.ARTICLE_ID = ? ";
QnaDTO qnaDTO = (QnaDTO) jdbcTemplate.queryForObject(
SQL,
new Object[]{articleID},
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
QnaDTO qnaDTO = new QnaDTO();
qnaDTO.setArticleID(rs.getInt("ARTICLE_ID"));
qnaDTO.setCategoryID(rs.getInt("CATEGORY_ID"));
qnaDTO.setCategoryName(rs.getString("CATEGORY_NAME"));
qnaDTO.setEmail1(rs.getString("EMAIL"));
qnaDTO.setTel1(rs.getString("TEL"));
qnaDTO.setSubject(rs.getString("SUBJECT"));
qnaDTO.setContests(rs.getString("CONTESTS"));
qnaDTO.setName(rs.getString("WRITER_NAME"));
qnaDTO.setUserID(rs.getString("WRITER_ID"));
//
qnaDTO.setReadcount(rs.getString("READCOUNT"));
qnaDTO.setAnswer(rs.getString("ANSWER"));
qnaDTO.setFileName(rs.getString("FILE_NAME"));
qnaDTO.setOpenFlg(rs.getString("OPEN_FLG"));
qnaDTO.setKtOpenFlg(rs.getString("KTOPEN_FLG"));
//
qnaDTO.setWriteDay(rs.getString("WRITE_DAY"));
qnaDTO.setDisposalFlg(rs.getString("DISPOSAL_FLG"));
return qnaDTO;
}
}
);
return qnaDTO;
}
As you can see above.
jdbcTemplate.queryForObject(...) is the method which really send Query And Get some result.
Inside of jdbcTemplate.queryForObject, finally logger used
public Object query(final String sql, final ResultSetExtractor rse)
throws DataAccessException
{
Assert.notNull(sql, "SQL must not be null");
Assert.notNull(rse, "ResultSetExtractor must not be null");
if(logger.isDebugEnabled())
logger.debug("Executing SQL query [" + sql + "]");
class _cls1QueryStatementCallback
implements StatementCallback, SqlProvider
{
public Object doInStatement(Statement stmt)
throws SQLException
{
ResultSet rs = null;
Object obj;
try
{
rs = stmt.executeQuery(sql);
ResultSet rsToUse = rs;
if(nativeJdbcExtractor != null)
rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
obj = rse.extractData(rsToUse);
}
finally
{
JdbcUtils.closeResultSet(rs);
}
return obj;
}
public String getSql()
{
return sql;
}
_cls1QueryStatementCallback()
{
super();
}
}
return execute(new _cls1QueryStatementCallback());
}
But with above sources, I could only get the SQL with ?.
What I want is that my result doesn't have question mark ?
It means filling ? with real data.
Is there any way to do this?
thanks

Jeon, sorry, was occupied with work. :-) Anyway, I have looked into your code and replicate here using spring 2.5. I've also google and I think you want to read this and this further to understand.
From the official documentation,
Finally, all of the SQL issued by this class is logged at the 'DEBUG'
level under the category corresponding to the fully qualified class
name of the template instance (typically JdbcTemplate, but it may be
different if a custom subclass of the JdbcTemplate class is being
used).
So you need to figure out how to enable logging with debug level.
Not sure exactly how you trace, but with my trace, I end up to below. So if you enable debug level, you should be able to see the output, maybe not exactly like QA.ARTICLE_ID = 123; but you should probably get the value printed in the next line something like in that example. Anyway, I don't have the exact setup like in your environment but this I think should you give a clue.
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)
throws DataAccessException {
Assert.notNull(psc, "PreparedStatementCreator must not be null");
Assert.notNull(action, "Callback object must not be null");
if (logger.isDebugEnabled()) {
String sql = getSql(psc);
logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
}

Related

Cassandra-quarkus-client hangs at quarkusCqlSession.prepare

I spotted problem with Quarkus CqlSession. When i calling method "prepare" its hang and timeout.
Code snippet:
#Inject
public NewsEngineRepository(QuarkusCqlSession quarkusCqlSession, NewsEngineConfig newsEngineConfig, Logger log) {
queryLimit = newsEngineConfig.dbQueryLimit();
this.quarkusCqlSession = quarkusCqlSession;
this.log = log;
SimpleStatement ss = SimpleStatement.newInstance("select " + ITEMID + ", " + PUBDATE + " from news_engine_items where defverid = ? AND group = ? Limit " + queryLimit);
preparedStatement= quarkusCqlSession.prepare(ss); // <---- hangs here
this.log.info("Prepared statement created successfully: " + ss.getQuery());
}
I found a two solutions:
Increasing property quarkus.vertx.eventLoopsPoolSize from 2 to 4
Setting properyty quarkus.cassandra.use-quarkus-event-loop to false
Can anyone explain why solution 1 works?

schemacrawlar can't print out table name

blow code just print out database name only,why?
public static void main(final String[] args) throws Exception
{
// Create a database connection
final DataSource dataSource = new DatabaseConnectionOptions("jdbc:mysql://localhost:3306/target_db");
final Connection connection = dataSource.getConnection("root", "password");
// Create the options
final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
options.setTableTypes(Lists.newArrayList("BASE TABLE","TABLE","VIEW"));
options.setRoutineInclusionRule(new ExcludeAll());
options.setSchemaInclusionRule(new RegularExpressionInclusionRule("target_db"));
options.setTableNamePattern("*");
// Get the schema definition
final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection, options);
for (final Schema schema : catalog.getSchemas())
{
System.out.print("c--> " + schema.getCatalogName() + "\n");
for (final Table table : catalog.getTables(schema))
{
System.out.print("o--> " + table);
if (table instanceof View)
{
System.out.println(" (VIEW)");
} else
{
System.out.println();
}
for (final Column column : table.getColumns())
{
System.out.println(" o--> " + column + " (" + column.getColumnDataType() + ")");
}
}
}
}
}
Strangely,
./schemacrawler.sh -server=mysql -database=target_db -user=root -password=password -infolevel=ALL -command=schema
will output tables and corresponded columns.
Update:my configuration
schemacrawler-14.09.03-main
Ubuntu 16.04 64bit
MariaDB 10.2.1-MariaDB-1~xenial
(I assumed mariadb may not be supported yet,so switch between blow two drivers,but neither works)
mysql-connector-java-6.0.3
mariadb-java-client-1.4.6
Finally,I figured it out:
options.setTableTypes(Lists.newArrayList("BASE TABLE","TABLE","VIEW","UNKNOWN"));
Caution:with MariaDB,table types is "UNKNOWN"

Processing 'Search' in an Arraylist

I'm stuck with the following problem within Processing for a school assignment.
I'm using a dataset in a tab seperated format, This is being read and parsed to my Activity class. The (menuAnswer, "Subagency") is used so I only get the data I need.
for(TableRow singleRow : trafficTable.findRows(menuAnswer, "SubAgency")){
Activity singleActivity = new Activity();
singleActivity.parseRow(singleRow);
activities.add(singleActivity);
}
The Activity class looks this:
class Activity{
String violationType;
String subAgency;
String race;
String gender;
Date readDate;
void parseRow(TableRow row){
this.subAgency = row.getString("SubAgency");
this.violationType = row.getString("Violation Type");
this.race = row.getString("Race");
this.gender = row.getString("Gender");
this.readDate = parseDate(row.getString("Time Of Stop") + " " + row.getString("Date"), "HH:mm:ss dd-MM-yyyy");
}
void printInfo(){
println(subAgency + " / " + race + " / " + readDate + " / " + violationType);
}
}
Every (usefull) piece of my dataset is inserted into an variable.
In my main class I want to search in the violationType String and count the ammount of "Warning"s within this String. I use the following code, which is not working:
for (Activity singleActivity : activities)
if(singleActivity.violationType == "Warning"){
warningCount++;
println("is it working?");
}
What am I doing wrong?
Mello
After searching I found my answer:
for (Activity singleActivity : activities)
if(singleActivity.violationType == "Warning"){
warningCount++;
println("is it working?");
}
My notation of my If statement is wrong and should be with .equals instead of == operator, like this:
void countWarning(){
for (Activity singleActivity : activities){
if(singleActivity.violationType.equals("Warning")){
warningCount++;
}
}
}

Strategy for logging in production for Dynamics CRM plugins

CRM 2011 on premise.
I have a plugin written in C#. It could throw exceptions or otherwise behave badly in production.
When this happens I want to capture information about the state and about recent code execution to help me analyse the problem.
Ideally I would like the following:
If the code decides I should know about a problem then I want it to be able to tell me that there was a problem asap, without me looking to see if a problem has occurred.
I want the information about the problem to be easily accessible to me. I don't want to have to RDP to another machine and search through files.
I don't want logging to have much impact on performance.
I guess I'm looking for something like this:
Retention of last n lines of log in memory.
A single function call to add a line to the log.
A single function call to cause an error to be recorded.
When there's an error, email to me saying there was a problem. This contains summary info.
The email contains a link to a web page showing me full error details. Stack trace, error message, date, time, user etc, last n lines of log.
It'd be nice if there was also a web page somewhere showing me all errors, with filtering, sorting and so on.
I'm fairly new to CRM but I've developed systems like this before. Since CRM has been around for many years I'd expect this to be available for it.
To support your wish list, I'd create these Entities:
PluginLog
Contains what ever information you'd like to retain for plugin calls that completed successfully
Is related to a Plugin Exception Class. This way you could look up what happened before
an exception occurred
PluginException
Contains any special information regarding the exception (user, context, stack trace)
Now let's go through your wish list:
Retention of last n lines of log in memory.
Not sure if you want to log the particular plugin class, or all plugins classes defined in the DLL, I'll assume a particular plugin class:
Create a static ConcurrentQueue for the plugin
A single function call to add a line to the log.
Create a single function that creates a PluginLog entity in memory (without creating it in the CRM database) and adds it to the queue.
If it's length > n, dequeue.
A single function call to cause an error to be recorded.
Again, this is something that you'll need to create. Basically I'd create a PLuginException Entity in CRM, then dequeue all the items off of the Queue, populating the Plugin Exception Id, and saving it to CRM
When there's an error, email to me saying there was a problem. This contains summary info.
As long as the App Domain context of the executing plugin has the rights needed (not sure if it does in CRM Online) this should be trivial.
The email contains a link to a web page showing me full error details. Stack trace, error message, date, time, user etc, last n lines of log.
You can create a link to the PluginException Entity Created, and include it in the e-mail along with all other pertinent information.
It'd be nice if there was also a web page somewhere showing me all errors, with filtering, sorting and so on.
Advanced Find to the rescue
Edit
To help get you started, this is what I currently use to retrieve all the information from the plugin context, and convert it into text that gets inserted into an Exception:
#region GetPluginInfo
private Exception GetPluginExecutionInfoForLog(IServiceProvider serviceProvider, Exception ex)
{
if(ex.GetType() == typeof(InvalidPluginExecutionException)){ return ex; }
try
{
var context = serviceProvider.GetContext();
ex = new InvalidPluginExecutionException(
String.Format("Error During Plugin Execution: {0}**** Context Values ****{0}{1}",
Environment.NewLine, GetPluginExecutionInfo(context)), ex);
}
catch (Exception childEx)
{
OnError(childEx);
}
return ex;
}
protected String GetPluginExecutionInfo(IPluginExecutionContext context)
{
var lines = new List<String>();
var target = GetTarget<Entity>(context);
lines.Add("MessageName: " + context.MessageName);
lines.Add("PrimaryEntityName: " + context.PrimaryEntityName);
lines.Add("PrimaryEntityId: " + context.PrimaryEntityId);
lines.Add("BusinessUnitId: " + context.BusinessUnitId);
lines.Add("CorrelationId: " + context.CorrelationId);
lines.Add("Depth: " + context.Depth);
lines.Add("Has Parent Context: " + (context.ParentContext != null));
lines.Add("InitiatingUserId: " + context.InitiatingUserId);
AddParameters(lines, context.InputParameters, "Input Parameters");
lines.Add("IsInTransaction: " + context.IsInTransaction);
lines.Add("IsolationMode: " + context.IsolationMode);
lines.Add("Mode: " + context.Mode);
lines.Add("OperationCreatedOn: " + context.OperationCreatedOn);
lines.Add("OperationId: " + context.OperationId);
lines.Add("Organization: " + context.OrganizationName + "(" + context.OrganizationId + ")");
AddParameters(lines, context.OutputParameters, "Output Parameters");
AddEntityReference(lines, context.OwningExtension, "OwningExtension");
AddEntityImages(lines, context.PostEntityImages, "Post Entity Images");
AddEntityImages(lines, context.PreEntityImages, "Pre Entity Images");
lines.Add("SecondaryEntityName: " + context.SecondaryEntityName);
AddParameters(lines, context.SharedVariables, "Shared Variables");
lines.Add("Stage: " + context.Stage);
lines.Add("UserId: " + context.UserId);
if (target == null || target.Attributes.Count == 0)
{
lines.Add("Target: Empty ");
}
else
{
lines.Add("* Target " + target.ToEntityReference().GetNameId() + " *");
foreach (var att in target.Attributes)
{
lines.Add(" Entity[" + att.Key + "]: " + GetAttributeValue(att.Value));
}
}
lines.Add("* App Config Values *");
foreach (var key in ConfigurationManager.AppSettings.AllKeys)
{
lines.Add(" [" + key + "]: " + ConfigurationManager.AppSettings[key]);
}
return String.Join(Environment.NewLine, lines);
}
private static string GetAttributeValue(object value)
{
if(value == null){
return "Null";
}
var type = value.GetType();
if (type == typeof(OptionSetValue))
{
return ((OptionSetValue)value).Value.ToString();
}
else if (type == typeof(EntityReference))
{
return ((EntityReference)value).GetNameId();
}
else
{
return value.ToString();
}
}
private static void AddEntityReference(List<string> nameValuePairs, EntityReference entity, string name)
{
if (entity != null)
{
nameValuePairs.Add(name + ": " + entity.GetNameId());
}
}
private static void AddEntityImages(List<string> nameValuePairs, EntityImageCollection images, string name)
{
if (images != null && images.Count > 0)
{
nameValuePairs.Add("** " + name + " **");
foreach (var image in images)
{
if (image.Value == null || image.Value.Attributes.Count == 0)
{
nameValuePairs.Add(" Image[" + image.Key + "] " + image.Value.ToEntityReference().GetNameId() + ": Empty");
}
else
{
nameValuePairs.Add("* Image[" + image.Key + "] " + image.Value.ToEntityReference().GetNameId() + " *");
foreach (var att in image.Value.Attributes)
{
nameValuePairs.Add(" Entity[" + att.Key + "]: " + GetAttributeValue(att.Value));
}
}
}
}
else
{
nameValuePairs.Add(name + ": Empty");
}
}
private static void AddParameters(List<string> nameValuePairs, ParameterCollection parameters, string name)
{
if (parameters != null && parameters.Count > 0)
{
nameValuePairs.Add("* " + name + " *");
foreach (var param in parameters)
{
nameValuePairs.Add(" Param[" + param.Key + "]: " + param.Value);
}
}
else
{
nameValuePairs.Add(name + ": Empty");
}
}
#endregion // GetPluginInfo
I've found a very quick way to do this at http://mscrmtech.com/mscrm-2011-useful-itracingservice-addiion-when-creating-plugin-assemblies/
It is documented (barely) here: http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.itracingservice.aspx
So I can do
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracingService == null)
throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
tracingService.Trace("CRMSTClasses.Main_Code.Execute - testing tracing");
throw new InvalidPluginExecutionException("Test exception");
And then the log file I can download from the error dialogue includes:
[CRMSTClasses: CRMSTClasses.Main_Code.Receiver]
[cb42fcb0-0717-e311-9097-0050569274a2: CRMSTClasses.Main_Code.Receiver: Create of incident]
CRMSTClasses.Main_Code.Execute - testing tracing
In the latest versions of Dynamics, the Plugin Trace Log captures a lot of this information simply by a user calling the pre-built Tracing Service. It doesn't enable proactive notifications as requested, but it has lots of other nice features. When you're ready to upgrade (if that's in the cards), I would recommend taking a look at it.

'id' could not be resolved ..?

i am using EF 4.1 and when I use the following code i get an error:
'id' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 6, column 1.
c#
foreach (Person Profile in _ProfilesRepository.GetProfiles(filterExpression.ToString(), sortingName, request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1))
{
//the error happens here when it comes back from the GetProfiles call : (
response.Records.Add(new JqGridRecord(Convert.ToString(Profile.ID), new List<object>()
{
Profile.Name,
Profile.JobTitle,
Profile.Type
}));
}
public IQueryable<Person> GetProfiles(string filterExpression, string sortExpression, string sortDirection, int pageIndex, int pageSize, int pagesCount)
{
if (!String.IsNullOrWhiteSpace(filterExpression))
return db.persons.Where(filterExpression).OrderBy(sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageSize);
else
return db.persons.OrderBy(sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pagesCount * pageSize);
//return db.persons;
}
The message appears when using orderBy("fieldname asc/desc")
db.persons.OrderBy(sortExpression + " " + sortDirection)
When I try to use orderBy using lambda expression it's working normally, no error message.
db.persons.OrderBy(x=>x.FieldName)
Try This, add "it" before your short expression/fieldname :
OrderBy("it."+sortExpression + " " + sortDirection).Skip(pageIndex *
PageSize).Take(pageSize); OrderBy("it."+sortExpression + " " +
sortDirection).Skip(pageIndex * pageSize).Take(pagesCount * pageSize);

Resources